Enums and Pattern Matching
CONCEPTS:Rust Enums and Pattern Matching
Question Variations
- "Why are Rust enums more expressive than C-style enums?"
- "What does exhaustive matching guarantee?"
- "When should you use `if let` rather than `match`?"
- "How does `Option<T>` use enums to model absence?"
Why This Is Asked
Enums and exhaustive matching are central to idiomatic Rust modeling. This question tests whether you can represent valid state explicitly and handle every case safely rather than relying on invalid sentinel values or nullable fields.
Key Concepts
- Enum variants can carry different shapes of associated data.
matcharms are checked for exhaustiveness.if letis concise when handling one pattern and ignoring the rest.- Pattern matching can destructure structs, tuples, references, and nested enum values.
Question Variations
- “Why are Rust enums more expressive than C-style enums?”
- “What does exhaustive matching guarantee?”
- “When should you use
if letrather thanmatch?” - “How does
Option<T>use enums to model absence?”