> For the complete documentation index, see [llms.txt](https://zenofchain.gitbook.io/sway-bian-cheng-yu-yan-geng-xin-zhong-the-sway-programming-languageupdatin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zenofchain.gitbook.io/sway-bian-cheng-yu-yan-geng-xin-zhong-the-sway-programming-languageupdatin/1.-dao-yan-introduction/1.5-biao-zhun-ku-standard-library.md).

# 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`](https://fuellabs.github.io/sway/v0.38.0/book/forc/commands/forc_new.html). 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`](https://fuellabs.github.io/sway/v0.38.0/book/forc/commands/forc_new.html)创建的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`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/prelude.sw), and re-exports the following:

当前版本的前置位于[`std::prelude`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/prelude.sw)，并重新输出以下内容：

* [`std::address::Address`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/address.sw), a wrapper around the `b256` type representing a wallet address.\
  [`std::address::Address`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/address.sw)，是对代表钱包地址的`b256`类型的封装器。
* [`std::contract_id::ContractId`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/contract_id.sw), a wrapper around the `b256` type representing the ID of a contract.\
  [`std::contract_id::ContractId`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/contract_id.sw), 围绕`b256`类型的包装，代表一个合约的ID。
* [`std::identity::Identity`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/identity.sw), an enum with two possible variants: `Address: Address` and `ContractId: ContractId`.\
  [`std::identity::Identity`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/identity.sw), 一个枚举，有两个可能的变体：: `Address: Address` 和 `ContractId: ContractId`.
* [`std::vec::Vec`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw), a growable, heap-allocated vector.\
  [`std::vec::Vec`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/vec.sw)，一个可增长的、用堆分配的向量。
* [`std::storage::storage_key::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_key.sw), contains the API for accessing a `core::storage::StorageKey` which describes a location in storage.\
  [`std::storage::storage_key::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_key.sw), 包含访问`core::storage::StorageKey`的API，描述存储中的一个位置。
* [`std::storage::storage_map::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_map.sw), a key-value mapping in contract storage.\
  [`std::storage::storage_map::*`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/storage/storage_map.sw), 是合约存储中的键值映射。
* [`std::option::Option`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw), an enum which expresses the presence or absence of a value.\
  [`std::option::Option`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw)，一个枚举，表达了一个值的存在或不存在。
* [`std::result::Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw), an enum for functions that may succeed or fail.\
  [`std::result::Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw), 一个表示可能成功或失败的函数的枚举。
* [`std::assert::assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM if the condition provided to it is `false`.\
  [`std::assert::assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), 一个函数，如果提供给它的条件是`false`，它将还原/回撤虚拟机。
* [`std::assert::assert_eq`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM and logs its two inputs `v1` and `v2` if the condition `v1` == `v2` is `false`.\
  [`std::assert::assert_eq`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw)，如果条件`v1`和`v2`是`false`，则该函数将还原虚拟机并记录其两个输入`v1`和`v2`。
* [`std::revert::require`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM and logs a given value if the condition provided to it is `false`.\
  [`std::revert::require`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw)，该函数用于还原/回撤虚拟机，如果提供给它的条件是`false`，则记录一个给定的值。
* [`std::revert::revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM.\
  [`std::revert::revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), 一个还原虚拟机的函数。
* [`std::logging::log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw), a function that logs arbitrary stack types.\
  [`std::logging::log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw), 一个记录任意栈类型的函数。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zenofchain.gitbook.io/sway-bian-cheng-yu-yan-geng-xin-zhong-the-sway-programming-languageupdatin/1.-dao-yan-introduction/1.5-biao-zhun-ku-standard-library.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
