Enums & Patterns
An enum (enum) is a type whose value is always exactly one variant from a list known in advance. In C or Java a variant is just a name or a number, while in Rust each variant can carry its own data: Write has a string, Move has two coordinates, Quit has nothing. A struct describes "all of these at once", an enum describes "one of these". Together they let you describe data so that a nonsensical state cannot even be written down.
The best-known enum is Option<T>. Rust has no null: a value that may be absent has the type Option<T> with the variants Some(value) and None. The compiler will not let you add a number to an Option<i32> until you have taken the value out and decided what to do about None — which is why the "accessed an empty reference" bug is caught in Rust at build time.
You take data out of enums with pattern matching. match compares a value against patterns from top to bottom, runs the first arm that fits, and checks that no variant is forgotten. When only one variant matters, you write if let, while let or let … else instead of a full match. Patterns themselves can do more than recognise a variant: |, ranges, guards, @ bindings and destructuring of nested values. This topic goes layer by layer — from declaring an enum to the pattern syntax.
Topic map
- Enums — variants with no data, tuple variants and variants with named fields; methods through
impl; a value is exactly one variant, not a set of constants as in C and Java; the size is the largest variant plus a tag. - The Option Type —
Some(T)orNoneinstead of null;Option<i32> + 1does not compile (E0369); a short list of methods:unwrap_or,map,and_then,ok_or,is_some. - The match Expression — arms are tried from top to bottom and the first one wins; data comes out of a variant right in the pattern; a missing variant is error
E0004; the_and named catch-alls; matching through a reference. - if let and let else — one arm instead of a whole
match;while letfor loops;let … elsetakes the value out or leaves; edition-2024 let chains; refutable patterns and errorE0005. - Pattern Syntax — literals,
|, ranges1..=9,ifguards,@bindings, destructuring of nested structs, tuples and enums,..for the rest.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Adding a number to an Option<i32> as if it were an i32 | error[E0369]: cannot add `{integer}` to `Option<i32>` : you first take the value out with match, if let or unwrap_or |
Calling unwrap() "because the value is definitely there" | On None — the panic called `Option::unwrap()` on a `None` value at runtime, not a build error |
Leaving a variant out of a match | error[E0004]: non-exhaustive patterns — the compiler names the forgotten variant |
Putting _ as the first arm, or closing your own enum with it "just in case" | The arms below are unreachable (unreachable pattern), and a new variant silently falls into _ instead of breaking the build |
Writing let Some(x) = opt; | error[E0005]: refutable pattern in local binding — you need let … else, if let or match |
Matching an Option<String> by value and then using it again | The string moves into the pattern: error[E0382]: borrow of partially moved value; match on &opt instead |
| Expecting guards to cover all cases | The compiler does not analyse them: E0004, you need an arm without a guard or _ |
Why it matters in interviews
Enums and match are the junior-level filter for "thinks in Rust's types" versus "writes Rust as if it were Java". The syntax takes an evening to learn, but questions about Option instead of null, the exhaustiveness of match and the difference between match, if let and let … else come up almost every time, and the middle-level round adds guards, @ and nested destructuring.
What interviewers usually check:
- How a Rust enum differs from an
enumin C and Java, and when an enum is a better fit than a struct. - Why Rust has no null and how
Option<T>forces you to handle a missing value. - How
matchworks, why it must be exhaustive, and how a_arm differs from a named arm. - When you write
if let,while letorlet … elseinstead ofmatch, and what you give up. - Why
let Some(x) = opt;does not compile: refutable and irrefutable patterns. - Guards,
@bindings, nested destructuring and..to skip the rest.