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项目导入一样。比如:

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 std::prelude, and re-exports the following:

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

Last updated