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
  • 使用标准库 (Using the Standard Library)
  • 标准库前置 (Standard Library Prelude)
  1. 1. 导言(Introduction)

1.5 标准库 (Standard Library)

Previous1.4 一个Forc项目 (A Forc Project)Next2. 示例(Example)

Last updated 2 years ago

Similar to Rust, Sway comes with its own standard library. 与Rust类似,Sway也有自己的标准库。

The Sway Standard Library is the foundation of portable Sway software, a set of minimal shared abstractions for the broader Sway ecosystem. It offers core types, like Result<T, E> and Option<T>, library-defined operations on language primitives, native asset management, blockchain contextual operations, access control, storage management, and support for types from other VMs, among many other things.

Sway标准库是可移植Sway软件的基础,是更广泛的Sway生态系统的一组最小共享的抽象。它提供了核心的类型,如Result<T, E>和Option<T>,库中定义的对语言基元的操作,原生资产管理,区块链环境的计算,访问控制,存储管理,以及对其他虚拟机的类型的支持,还有其他许多事情。

The entire Sway standard library is a Forc project called std, and is available directly here: (navigate to the appropriate tagged release if the latest master is not compatible). For the latest std documentation see: .

整个Sway标准库是一个名为 std的Forc项目,可以直接在这里获得: (如果最新的master不兼容,请导航到相应的标记版本)。最新的std文档见: 。

使用标准库 (Using the Standard Library)

The standard library is made implicitly available to all Forc projects created using . In other words, it is not required to manually specify std as an explicit dependency. Forc will automatically use the version of std that matches its version.

所有使用创建的Forc项目都可以隐含地使用标准库。换句话说,不需要手动指定std作为明确的依赖关系。Forc将自动使用与其版本相匹配的std版本。

Importing items from the standard library can be done using the use keyword, just as importing items from any Sway project. For example:

从标准库导入项目可以使用use关键字,就像从任何Sway项目导入一样。比如:

use std::storage::storage_vec::*;

This imports the StorageVec type into the current namespace.

如此就把StorageVec类型导入到当前命名空间。

标准库前置 (Standard Library Prelude)

Sway comes with a variety of things in its standard library. However, if you had to manually import every single thing that you used, it would be very verbose. But importing a lot of things that a program never uses isn't good either. A balance needs to be struck.

Sway在其标准库中带有各种东西。然而,如果你不得不手动导入你所使用的每一个东西,那将非常冗长。但是,导入大量程序从未使用过的东西也不是好事。需要取得一种平衡。

The prelude is the list of things that Sway automatically imports into every Sway program. It's kept as small as possible, and is focused on things which are used in almost every single Sway program.

前置是Sway自动导入每个Sway程序的东西的列表。它被保持在尽可能小的范围内,并且集中在几乎每个Sway程序都会用到的东西上。

The current version of the prelude lives in , and re-exports the following:

当前版本的前置位于,并重新输出以下内容:

, a wrapper around the b256 type representing a wallet address. ,是对代表钱包地址的b256类型的封装器。

, a wrapper around the b256 type representing the ID of a contract. , 围绕b256类型的包装,代表一个合约的ID。

, an enum with two possible variants: Address: Address and ContractId: ContractId. , 一个枚举,有两个可能的变体:: Address: Address 和 ContractId: ContractId.

, a growable, heap-allocated vector. ,一个可增长的、用堆分配的向量。

, contains the API for accessing a core::storage::StorageKey which describes a location in storage. , 包含访问core::storage::StorageKey的API,描述存储中的一个位置。

, a key-value mapping in contract storage. , 是合约存储中的键值映射。

, an enum which expresses the presence or absence of a value. ,一个枚举,表达了一个值的存在或不存在。

, an enum for functions that may succeed or fail. , 一个表示可能成功或失败的函数的枚举。

, a function that reverts the VM if the condition provided to it is false. , 一个函数,如果提供给它的条件是false,它将还原/回撤虚拟机。

, a function that reverts the VM and logs its two inputs v1 and v2 if the condition v1 == v2 is false. ,如果条件v1和v2是false,则该函数将还原虚拟机并记录其两个输入v1和v2。

, a function that reverts the VM and logs a given value if the condition provided to it is false. ,该函数用于还原/回撤虚拟机,如果提供给它的条件是false,则记录一个给定的值。

, a function that reverts the VM. , 一个还原虚拟机的函数。

, a function that logs arbitrary stack types. , 一个记录任意栈类型的函数。

https://github.com/FuelLabs/sway/tree/master/sway-lib-std
https://fuellabs.github.io/sway/master/std/
https://github.com/FuelLabs/sway/tree/master/sway-lib-std
https://fuellabs.github.io/sway/master/std/
forc new
forc new
std::prelude
std::prelude
std::address::Address
std::address::Address
std::contract_id::ContractId
std::contract_id::ContractId
std::identity::Identity
std::identity::Identity
std::vec::Vec
std::vec::Vec
std::storage::storage_key::*
std::storage::storage_key::*
std::storage::storage_map::*
std::storage::storage_map::*
std::option::Option
std::option::Option
std::result::Result
std::result::Result
std::assert::assert
std::assert::assert
std::assert::assert_eq
std::assert::assert_eq
std::revert::require
std::revert::require
std::revert::revert
std::revert::revert
std::logging::log
std::logging::log