1.5 标准库 (Standard Library)
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: https://github.com/FuelLabs/sway/tree/master/sway-lib-std (navigate to the appropriate tagged release if the latest master
is not compatible). For the latest std
documentation see: https://fuellabs.github.io/sway/master/std/.
整个Sway标准库是一个名为 std
的Forc项目,可以直接在这里获得: https://github.com/FuelLabs/sway/tree/master/sway-lib-std (如果最新的master
不兼容,请导航到相应的标记版本)。最新的std
文档见: https://fuellabs.github.io/sway/master/std/。
使用标准库 (Using the Standard Library)
The standard library is made implicitly available to all Forc projects created using forc new
. 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 new
创建的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项目导入一样。比如:
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 std::prelude
, and re-exports the following:
当前版本的前置位于std::prelude
,并重新输出以下内容:
std::address::Address
, a wrapper around theb256
type representing a wallet address.std::address::Address
,是对代表钱包地址的b256
类型的封装器。std::contract_id::ContractId
, a wrapper around theb256
type representing the ID of a contract.std::contract_id::ContractId
, 围绕b256
类型的包装,代表一个合约的ID。std::identity::Identity
, an enum with two possible variants:Address: Address
andContractId: ContractId
.std::identity::Identity
, 一个枚举,有两个可能的变体::Address: Address
和ContractId: ContractId
.std::vec::Vec
, a growable, heap-allocated vector.std::vec::Vec
,一个可增长的、用堆分配的向量。std::storage::storage_key::*
, contains the API for accessing acore::storage::StorageKey
which describes a location in storage.std::storage::storage_key::*
, 包含访问core::storage::StorageKey
的API,描述存储中的一个位置。std::storage::storage_map::*
, a key-value mapping in contract storage.std::storage::storage_map::*
, 是合约存储中的键值映射。std::option::Option
, an enum which expresses the presence or absence of a value.std::option::Option
,一个枚举,表达了一个值的存在或不存在。std::result::Result
, an enum for functions that may succeed or fail.std::result::Result
, 一个表示可能成功或失败的函数的枚举。std::assert::assert
, a function that reverts the VM if the condition provided to it isfalse
.std::assert::assert
, 一个函数,如果提供给它的条件是false
,它将还原/回撤虚拟机。std::assert::assert_eq
, a function that reverts the VM and logs its two inputsv1
andv2
if the conditionv1
==v2
isfalse
.std::assert::assert_eq
,如果条件v1
和v2
是false
,则该函数将还原虚拟机并记录其两个输入v1
和v2
。std::revert::require
, a function that reverts the VM and logs a given value if the condition provided to it isfalse
.std::revert::require
,该函数用于还原/回撤虚拟机,如果提供给它的条件是false
,则记录一个给定的值。std::revert::revert
, a function that reverts the VM.std::revert::revert
, 一个还原虚拟机的函数。std::logging::log
, a function that logs arbitrary stack types.std::logging::log
, 一个记录任意栈类型的函数。
Last updated