Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

9.1 枚举类型(Enum)

Move 的枚举实现

Move 没有原生 enum,但可以用 sum type(联合体)模式实现。

枚举的定义与使用

module my_addr::enum_example {
    struct MyEnum has copy, drop, store {
        tag: u8, // 0: A, 1: B
        value_a: u64,
        value_b: bool,
    }
    // ...示例代码...
}

枚举的常见用法

  • 状态机
  • 错误码
  • 事件类型

小结

枚举模式让 Move 代码更具表达力。