Enums
#[derive(Debug)] enum Message { Quit, // Unit-like variant (no fields) Move { x: i32, y: i32 }, // Struct-like variant (named fields) Write(String), // Tuple-like variant (numbered fields) ChangeColor(i32, i32, i32), // Another tuple-like variant } // Define methods on enums. impl Message { fn call(&self) { // method body would be defined here } } fn main() { // `msg` is assigned one of the variants. // Note the :: between the name of the type and the name of the variant. let msg = Message::Quit; println!("{msg:?}"); // or let msg = Message::Move { x: 10, y: 15 }; println!("{msg:?}"); // or let msg = Message::ChangeColor(127, 0, 0); println!("{msg:?}"); }
If we make an enum public, all of its variants are then public. We only need pub
⮳ before the enum
⮳ keyword.
strum
strum
⮳ provides helpful macros for working with enums and strings.
Common enums
- Option.
- Result.
Related Topics
- Match.
- Rust Patterns.
- Functional Programming.
- Simple Data Types.
- Structs.