The whole problem with the world is that fools and fanatics are always so certain of themselves, and wiser people so full of doubts.
— Bertrand Russell
Enums and Pattern Matching
NAME wyzer-enums - Enumerated types and exhaustive pattern matching
DESCRIPTION
Enums define a type consisting of multiple variants. Variants can be tagged with explicit integral values via iota or contain complex data payloads.
SYNOPSIS: ENUMS AND IOTA
enum Flag {
None = 0,
Read = 1 << iota,
Write = 1 << iota,
}
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(str),
}
ENUM DETAILS
iota: A built-in compile-time counter that automatically increments per variant.- Data Variants: Variants can enclose structures (
Move { x, y }) or tuples (Write(str)).
SYNOPSIS: PATTERN MATCHING
fn process(msg: Message) {
match msg {
Message::Quit => { /* ... */ },
Message::Move { x, y } => { /* ... */ },
Message::Write(text) => { /* ... */ },
}
}
MATCH DETAILS
match: Branches execution based on the enum variant.- Exhaustiveness: The compiler strictly verifies that all possible variants are explicitly handled.