Start with the idea
match forces every variant to be handled, so adding a new state turns every incomplete match into a compile error instead of a silent bug. Variants can carry data, which lets one type model a whole state machine rather than a bag of loosely related fields.
Read the code
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
}
let message = Message::Move { x: 2, y: 3 };
match message {
Message::Quit => println!("quit"),
Message::Move { x, y } => {
println!("move {x},{y}")
}
Message::Write(t) => println!("{t}"),
}The Move value carries coordinates. A match must cover every variant unless a catch-all deliberately covers the remaining cases.
Practice starter
match state {
State::Idle => run(),
_ => {}
}
Work through it
A boolean can distinguish two states, but it becomes obscure when a feature has several states or each state carries different data. An enum names the alternatives. Message::Quit carries nothing, Message::Move carries two coordinates, and Message::Write carries text. A value is exactly one variant at a time, so code cannot accidentally pair coordinates with a quit message as it might with a loose struct of optional fields.
A match examines the variant and binds only the data available in that branch. Every arm must produce a compatible type if the match is used as an expression. The compiler's exhaustiveness check is valuable during maintenance: adding Message::Resize points to every decision that now needs a policy. A wildcard _ can be appropriate when all remaining cases truly share the same behavior, but it hides newly added variants if used merely to silence the compiler. For a small state machine, spell out the known cases.
Trace the sample with one value of each variant. Note that Message::Write(t) moves its String out when the enum is matched by value; match a reference, match &message, if you need to keep the original message. Then add a variant with a field and update the rendering match explicitly. This exercise connects data modeling, exhaustive control flow, and ownership. A well-shaped enum makes invalid combinations harder to represent and changes to the model easier to audit.
When the enum is public, adding a variant may require downstream users to update their exhaustive matches. That compile-time interruption is useful: it forces every caller to consider the new state rather than silently treating it as an old one.
An enum is one value chosen from several named variants. A variant can carry data, so Message::Write(String) and Message::Quit have different shapes under one type. A match can handle each shape explicitly. The compiler checks that every possible variant is covered unless you use a wildcard; that guarantee is especially useful when a new variant is added later.
For each variant in the example, write down what data it carries. Then trace a match: which arm runs, which fields are bound, and what value the arm produces. Add one new variant and inspect the exhaustiveness error before writing its behavior. Avoid a wildcard when it would hide an important new case. Enums are useful when exactly one state can be active at a time; several independent booleans often allow impossible combinations. Make invalid combinations unrepresentable where the domain rules are stable.
Try it
Match every Signal variant: Red lasts 30 seconds, Amber 5, Green 20. In the Crabrix app, open the compiler lab and complete the supplied function in solution.rs. The reading question is a separate exercise.
What success looks like: The local compiler accepts the changed solution and the separate verification harness finishes all normal and boundary cases. Printing a success message alone does not complete this lab.
Check your understanding
What does the wildcard cost you?
Reveal the answer
- Nothing — a wildcard arm keeps the match exhaustive
- A new variant silently falls into it instead of erroring
- It makes the match slower by adding a runtime check
A catch-all removes the compiler's help exactly when you add a state.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Enums & Match (courseId "basics", lessonId "enums") Link: https://crabrix.com/learn/basics/enums/ Goal: match is exhaustive: every variant must be handled. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/enums) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.