Skip to content

10. 标准库

Move 标准库 (std) 提供了一组核心模块,包含常用类型和函数,供 Move 程序使用。这些模块涵盖了基础功能,例如字符串操作、向量操作、选项类型以及基本类型的处理。

模块概述

标准库包含以下模块:

  • std::string:提供基本的字符串操作,包括字符串的创建、连接、分割和长度计算。
  • std::ascii:支持基本的 ASCII 字符串操作。
  • std::option:实现 Option<T> 类型,用于表示值的存在或缺失。
  • std::vector:提供对向量类型的原生操作,例如添加、删除元素,获取长度以及检查是否为空。
  • std::bcs:包含 bcs::to_bytes() 函数,用于将 Move 值序列化为字节序列。
  • std::address:提供 address::length() 函数,用于获取地址的长度。
  • std::type_name:支持运行时类型反射,允许获取类型的名称、模块和地址。
  • std::hash:提供哈希函数,包括 sha2_256sha3_256
  • std::debug:包含调试函数,仅在测试模式下可用。
  • std::bit_vector:提供对位向量的操作。
  • std::fixed_point32:提供 FixedPoint32 类型,用于定点数计算。

常用模块示例

以下是对常用模块的功能介绍及示例代码,帮助开发者更直观地理解标准库的应用。

std::string 模块

功能: 支持字符串的创建、连接和基本操作。

示例代码:

move
use std::string;

public fun demo_string_operations() {
    let hello = string::utf8(b"Hello");
    let world = string::utf8(b"World");

    // 字符串连接
    let hello_world = string::concat(&hello, &string::utf8(b" ") );
    let hello_world = string::concat(&hello_world, &world);
    assert!(string::length(&hello_world) == 11, 1);

    // 检查前缀
    let is_hello_prefix = string::starts_with(&hello_world, &hello);
    assert!(is_hello_prefix, 2);
}

std::vector 模块

功能: 提供向量的创建、修改和查询。

示例代码:

move
use std::vector;

public fun demo_vector_operations() {
    let mut nums = vector::empty<u64>();
    
    // 添加元素
    vector::push_back(&mut nums, 10);
    vector::push_back(&mut nums, 20);
    vector::push_back(&mut nums, 30);

    // 查询向量
    assert!(vector::length(&nums) == 3, 1);
    assert!(vector::contains(&nums, 20), 2);

    // 获取元素
    let first = *vector::borrow(&nums, 0);
    assert!(first == 10, 3);

    // 删除元素
    let last = vector::pop_back(&mut nums);
    assert!(last == 30, 4);
}

std::option 模块

功能: 表示值可能存在(some)或不存在(none)。

示例代码:

move
use std::option::{self, Option};

public fun demo_option_usage() {
    let value: Option<u64> = option::some(42);

    // 解构 Option
    match value {
        option::some(x) => {
            assert!(x == 42, 1);
        },
        option::none => {
            assert!(false, 2); // 永远不会到这里
        },
    };

    // 创建 none
    let no_value: Option<u64> = option::none();
    assert!(option::is_none(&no_value), 3);
}

std::hash 模块

功能: 提供常用的哈希算法。

示例代码:

move
use std::hash;

public fun demo_hash_functions() {
    let data = b"Move is awesome!";

    // 使用 SHA2_256 哈希
    let hash1 = hash::sha2_256(data);
    assert!(hash1[0] != 0, 1); // 哈希值应该有内容

    // 使用 SHA3_256 哈希
    let hash2 = hash::sha3_256(data);
    assert!(hash2[0] != 0, 2); // 哈希值应该有内容
}

std::bcs 模块

功能: 提供将 Move 值序列化为字节数组的功能。

示例代码:

move
use std::bcs;

public fun demo_bcs_serialization() {
    let value = 123u64;
    let serialized = bcs::to_bytes(&value);

    // 检查序列化结果
    assert!(serialized.length() > 0, 1);
}

整数模块

标准库还提供了一组与整数类型相关的函数。这些函数被分成多个模块,每个模块都与特定的整数类型相关联。这些模块不应直接导入,但它们的函数在每个整数值上都可用。

所有模块都提供相同的函数集,包括:maxdiffdivide_and_round_upsqrtpow

隐式导入

某些模块是隐式导入的,无需显式 use 即可在模块中使用。这些模块包括:

  • std::vector
  • std::option
  • std::option::Option

隐式导入的模块和类型可以直接在代码中使用。

导入 std 但不导入 Sui 框架

Move 标准库可以直接导入到包中。但是,单独使用 std 不足以构建有意义的应用程序,因为它不提供任何存储功能,也不能与链上状态交互。

总结

Move 标准库提供了一组强大的基础工具和类型,支持字符串、向量、选项类型、哈希等基本功能。通过对这些模块的深入了解和实践,开发者可以编写高效、可维护的 Move 代码,从而构建更复杂和强大的应用程序。