> For the complete documentation index, see [llms.txt](https://chihaolu.gitbook.io/all-in-one-solidity/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chihaolu.gitbook.io/all-in-one-solidity/part-i-basic/chapter-6-zi-liao-jie-gou-data-structures/enum.md).

# 【Enum】

枚舉（`enum`）和 `struct` 非常類似，只是 `enum` 中的每個參數會自動從 0 開始依序初始化。

Enum具有以下的性質：

* 內部已有整數型態的元素存在
* 可以存進uint8的型態，如果數值數量介於0\~255，若大於256個值則可宣告為uint16

```solidity
enum ActionChoices { GoLeft, GoRight, GoStraight, SitsStill }
ActionChoices choice;
ActionChoices constant defaultChoice = ActionChoices.Gostraight;
```

在 Solidity 中 `enum` 更常被用來進行模型選擇（model choice），或者追蹤狀態（track of state）。

```solidity
pragma solidity ^0.8.11;

contract MyEnums {
    
    enum Rarity {
        original, // 0
        rare, // 1
        super_rare // 2
    }
    
    Rarity public rarity;
    
    constructor() {
        rarity = Rarity.rare;
    }
    
    // 我們可以直接將一個型態為該 enum 的變數賦值為某特定屬性
    function makeSuperRare() public {
        rarity = Rarity.super_rare;
    }
    
    // 我們可以藉由 enum 初始化的 INDEX 來進行賦值
    function set(Rarity _rarity) public {
        rarity = _rarity;
    }
    
    // 我們可以藉由 delete 來重置這個 enum 成他的初始值 0
    function reset() public {
        delete rarity;
    }
}
```

`struct` 和 `enum` 的差別：

* Struct 用於表示一個複雜變數
* Enum 用於表示一個單一變數的不同狀態或值


---

# 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://chihaolu.gitbook.io/all-in-one-solidity/part-i-basic/chapter-6-zi-liao-jie-gou-data-structures/enum.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.
