Topic
Enums
An enum defines a type that can be exactly one of a fixed set of values. Each option is called a variant. Variants can hold no data, tuple-like data, or struct-like named fields - all in the same enum. This makes enums more powerful than simple flags. You use match to run different code depending on which variant is active.
Quick Reference
| Concept | What it means |
|---|---|
| enum | A custom type where only ONE value is active at a time. |
| Variant | One of the possible values inside an enum. |
| EnumName::Variant | Two colons (::) connect the enum name to the variant you picked. |
| Discriminator | A number Rust assigns to each variant (starting at 0). You can set your own. |
| match | A way to say "if this variant, do this; if that variant, do that." |
| _ catch-all | An underscore arm in match that handles every variant not named explicitly. |
| Term | What it means | When you use it |
|---|---|---|
| enum | A custom type where only ONE value is active at a time | When a value can only be one of a fixed set of states |
| Variant | One of the possible values inside an enum | The specific state an enum instance holds right now |
| EnumName::Variant | Two colons (::) connect the enum name to the variant you picked | Every time you create or match against an enum value |
| Discriminator | A number Rust assigns to each variant, starting at 0 | When storing an enum value as a number, like saving state on-chain |
| match | A way to run different code for each variant | When your code must respond differently depending on which variant is active |
| _ catch-all | An underscore arm that handles every variant not named explicitly | When you only care about a few variants and want to ignore the rest |