Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  • 通过预编译的二进制文件进行安装(推荐)-Install via Pre-compiled Binaries (Recommended)
  • 从源码安装 (Installing from Source)
  • 启用Bash、Fish、Zsh或PowerShell的标签完成功能 (Enable tab completion for Bash, Fish, Zsh, or PowerShell)
  1. 1. 导言(Introduction)

1.1 安装(Installation)

Previous1. 导言(Introduction)Next1.2 Sway快速入门 (Sway Quickstart)

Last updated 2 years ago

The Sway toolchain is sufficient to compile Sway smart contracts. Otherwise, note that if you want to run Sway smart contracts (e.g. for testing), a Fuel Core full node is required, which is packaged together with the Sway toolchain together as the Fuel toolchain.

Sway工具链 足以编译Sway智能合约。否则,请注意,如果你想运行Sway智能合约(例如用于测试),需要一个Fuel Core完整节点,它与 Sway工具链 一起打包为 Fuel工具链。

通过预编译的二进制文件进行安装(推荐)-Install via Pre-compiled Binaries (Recommended)

Installing via pre-compiled release binaries is the recommended way to get up and running with the Sway toolchain. Pre-compiled binaries for Linux and macOS are available. Native Windows is currently unsupported (). Windows Subsystem for Linux should work but is not officially supported.

使用Sway工具链启动和运行的推荐方式是通过预编译的二进制文件进行安装。Linux和macOS的预编译二进制文件已经可用。本地Windows目前不支持()。用于Linux的Windows子系统应该可以工作,但不被官方支持。

依赖 (Dependencies)

A prerequisite for installing and using Sway is the Rust toolchain. Platform-specific instructions for installing rustup can be found . Then, install the Rust toolchain with:

安装和使用Sway的先决条件是Rust工具链。关于安装rustup的特定平台说明可以在找到。然后,用以下方法安装Rust工具链:

# Install the latest stable Rust toolchain.
rustup install stable

The Sway toolchain is built and tested against the stable Rust toolchain version (). There is no guarantee it will work with the nightly Rust toolchain, or with earlier stable versions, so ensure you are using stable with:

Sway工具链是根据stable Rust工具链版本()构建和测试的。不保证它能在nightly Rust工具链或早期的 stable 版本中工作,所以要确保你使用的是stable:

# Update installed Rust toolchain; can be used independently.
rustup update
# Set the stable Rust toolchain as default; can be used independently.
rustup default stable

Now you're ready to install , the equivalent of Rust's rustup for the Fuel toolchain. It enables easily downloading binary releases of the Fuel toolchain.

  1. Start by installing fuelup with the following command: 用以下命令安装fuelup:

curl --proto '=https' --tlsv1.2 -sSf \
https://install.fuel.network/fuelup-init.sh | sh

This downloads the fuelup-init script to a temp directory on your machine, which installs fuelup. fuelup-init will ask for permission to add ~/.fuelup/bin to your PATH. Otherwise, you can also pass --no-modify-path so that fuelup-init does not modify your PATH:

这将下载fuelup-init脚本到你机器上的一个临时目录,从而安装fuelup。fuelup-init会要求你将~/.fuelup/bin加入你的PATH中。当然,你也可以使用--no-modify-path,这样fuelup-init就不会修改你的PATH:

curl --proto '=https' --tlsv1.2 -sSf \
https://install.fuel.network/fuelup-init.sh | sh -s -- --no-modify-path
  1. Once fuelup is installed, fuelup-init automatically runs fuelup toolchain install latest to install the latest toolchain. 一旦安装完 fuelup, fuelup-init 会自动运行 fuelup toolchain install latest 来安装最新的工具链。

    You can run fuelup update at anytime to get the most up-to-date toolchain. 你可以在任何时候运行 fuelup update 来获得最新的工具链。

  2. (Optional) You can optionally install distributed toolchains optimized for different networks. (可选)你可以选择安装为不同网络优化的分布式工具链。 To configure the optimal toolchain for beta-3, run the following commands: 要配置beta-3的最佳工具链,请运行以下命令:

$ fuelup self update
Fetching binary from https://github.com/FuelLabs/fuelup/releases/download/v0.18.0/fuelup-0.18.0-aarch64-apple-darwin.tar.gz
 Downloading component fuelup without verifying checksum
 Unpacking and moving fuelup to /var/folders/tp/0l8zdx9j4s9_n609ykwxl0qw0000gn/T/.tmpP3HfvR
 Moving /var/folders/tp/0l8zdx9j4s9_n609ykwxl0qw0000gn/T/.tmpP3HfvR/fuelup to /Users/user/.fuelup/bin/fuelup


$ fuelup toolchain install beta-3
Downloading: forc forc-explore forc-wallet fuel-core fuel-indexer

Adding component forc v0.35.0 to 'beta-3-aarch64-apple-darwin'
Fetching binary from https://github.com/FuelLabs/sway/releases/download/v0.35.0/forc-binaries-darwin_arm64.tar.gz
npacking and moving forc-doc to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Unpacking and moving forc to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Unpacking and moving forc-deploy to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Unpacking and moving forc-run to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Unpacking and moving forc-lsp to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Unpacking and moving forc-fmt to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Fetching core forc dependencies
Installed forc v0.35.0 for toolchain 'beta-3-aarch64-apple-darwin'

Adding component forc-explore v0.28.1 to 'beta-3-aarch64-apple-darwin'
Fetching binary from https://github.com/FuelLabs/forc-explorer/releases/download/v0.28.1/forc-explore-0.28.1-aarch64-apple-darwin.tar.gz
Unpacking and moving forc-explore to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Installed forc-explore v0.28.1 for toolchain 'beta-3-aarch64-apple-darwin'

Adding component forc-wallet v0.1.3 to 'beta-3-aarch64-apple-darwin'
Fetching binary from https://github.com/FuelLabs/forc-wallet/releases/download/v0.1.3/forc-wallet-0.1.3-aarch64-apple-darwin.tar.gz
Unpacking and moving forc-wallet to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Installed forc-wallet v0.1.3 for toolchain 'beta-3-aarch64-apple-darwin'

Adding component fuel-core v0.17.1 to 'beta-3-aarch64-apple-darwin'
Fetching binary from https://github.com/FuelLabs/fuel-core/releases/download/v0.17.1/fuel-core-0.17.1-aarch64-apple-darwin.tar.gz
Unpacking and moving fuel-core to /Users/user/.fuelup/toolchains/    beta-3-aarch64-apple-darwin/bin
Installed fuel-core v0.17.1 for toolchain 'beta-3-aarch64-apple-darwin'

Adding component fuel-indexer v0.2.3 to 'beta-3-aarch64-apple-darwin'
Fetching binary from https://github.com/FuelLabs/fuel-indexer/releases/download/v0.2.3/fuel-indexer-0.2.3-aarch64-apple-darwin.tar.gz
Unpacking and moving fuel-indexer to /Users/user/.fuelup/toolchains/beta-3-aarch64-apple-darwin/bin
Installed fuel-indexer v0.2.3 for toolchain 'beta-3-aarch64-apple-darwin'

Installed:
- forc 0.35.0
- forc-explore 0.28.1
- forc-wallet 0.1.3
- fuel-core 0.17.1
- fuel-indexer 0.2.3

The Fuel toolchain is installed and up to date

You're all set to start building! 现在你已经准备好开始构建了!

需要帮助? (Need Help?)

从Cargo中安装 (Installing from Cargo)

The Sway toolchain and Fuel Core full node can be installed from source with Cargo with:

Sway工具链和Fuel Core全节点可以通过Cargo从源码安装:

cargo install forc fuel-core

从Cargo中更新forc (Updating forc from Cargo)

You can update the toolchain from source with Cargo with:

你可以用Cargo从源代码中更新工具链:

cargo update forc fuel-core

从Cargo中安装forc插件 (Installing forc Plugins from Cargo)

The Fuel ecosystem has a few plugins which can be easily installed via Cargo. Fuel生态系统有一些插件,可以通过Cargo轻松安装。

Note: forc detects anything in your $PATH prefixed with forc- as a plugin. Use forc plugins to see what you currently have installed. 注意: 作为一个插件, forc会检测你的$PATH中任何以forc-为前缀的东西。使用 forc plugins 来查看你目前安装了什么。

# Sway Formatter
cargo install forc-fmt

# Block Explorer
cargo install forc-explore

# Sway Language Server
cargo install forc-lsp

从源码安装 (Installing from Source)

启用Bash、Fish、Zsh或PowerShell的标签完成功能 (Enable tab completion for Bash, Fish, Zsh, or PowerShell)

forc supports generating completion scripts for Bash, Fish, Zsh, and PowerShell. See forc completions --help for full details, but the gist is as simple as using one of the following:

forc支持为Bash、Fish、Zsh和PowerShell生成完成脚本。完整的细节见forc completions --help,但要点是使用以下其中一个就可以了:

# Bash
forc completions --shell=bash > ~/.local/share/bash-completion/completions/forc

# Bash (macOS/Homebrew)
forc completions --shell=bash > $(brew --prefix)/etc/bash_completion.d/forc.bash-completion

# Fish
mkdir -p ~/.config/fish/completions
forc completions --shell=fish > ~/.config/fish/completions/forc.fish

# Zsh
forc completions --shell=zsh > ~/.zfunc/_forc

# PowerShell v5.0+
forc completions --shell=powershell >> $PROFILE.CurrentUserCurrentHost
# or
forc completions --shell=powershell | Out-String | Invoke-Expression

Once the completions have been generated and properly installed, close and reopen your terminal for the new completions to take effect.

一旦完整生成并正确安装,重启你的终端,新的安装就会生效。

现在你准备安装,相当于Rust的 rustup,它用于Fuel工具链。它可以轻松下载Fuel工具链的二进制版本。

You may refer to for an in-depth look into fuelup, or check out the tooling section in the if you're running into problems through the installation process. If you don't see your question, post the issue you're running into with as many details as possible and the team will get back to you asap!

如果你在安装过程中遇到问题,可以参考来深入了解fuelup,或者查看中的工具部分。如果你没有看到你的问题,请把你遇到的问题和尽可能多的细节贴出来,我们的团队会尽快给你答复!

Rather than installing from cargo, the Sway toolchain can be built from a local source checkout by following instructions at . The Fuel Core full node implementation can be built from source by following instructions at .

与其从 cargo安装,Sway工具链可以按照的说明,从本地源码签出来构建。Fuel Core的全节点实现可以按照的说明从源码中构建。

tracking issue for Windows support
跟踪Windows支持问题
here
这里
https://github.com/rust-lang/rust/releases/latest
https://github.com/rust-lang/rust/releases/latest
fuelup
fuelup
The Fuelup Book
Fuel forum
Fuelup Book
Fuel 论坛
https://github.com/FuelLabs/sway
https://github.com/FuelLabs/fuel-core
https://github.com/FuelLabs/sway
https://github.com/FuelLabs/fuel-core