Start with the idea
Patterns work in match, if let, while let, function parameters, and plain let. Guards add a condition, @ binds a value while testing it, and | matches several shapes at once. The compiler checks exhaustiveness, so a pattern set that misses a case is an error rather than a surprise at runtime.
Read the code
let point = (3, -7);
match point {
(0, 0) => println!("origin"),
(x, 0) | (0, x) => println!("axis at {x}"),
(x, y) if y < 0 => println!("below: {x},{y}"),
(x, y) => println!("somewhere: {x},{y}"),
}One arm can test, bind, and destructure at the same time.
Practice starter
let value = Some(4);
match value {
Some(n) if n % 2 == 0 => println!("even {n}"),
Some(n) => println!("odd {n}"),
None => println!("none"),
}
Work through it
A pattern describes the shape of a value and may bind its parts. In the tuple example, (0, 0) recognizes one exact point, (x, 0) | (0, x) recognizes either axis, and (x, y) if y < 0 adds a guard. Arms run top to bottom: the first matching arm wins. That order matters because a broad arm placed early can make a later, more specific arm unreachable. Read a match as a decision table, then test one input per row and one input at a boundary.
Patterns also appear outside match. let (width, height) = size; destructures a tuple directly when the shape always fits. if let Some(value) = maybe_value handles a single interesting enum case and lets the rest continue. while let Some(item) = stack.pop() repeats until the pattern no longer fits. Use a full match when every case deserves an explicit policy or when you want the compiler to point out a newly added variant.
A guard is checked after the pattern binds names. It narrows an arm, but it does not generally count as covering every possible value of that shape. In the practice example, an even Some(n) goes to the guarded arm, an odd one to the next Some(n) arm, and None to its own arm. Remove the second Some arm and observe the exhaustiveness error. That exercise shows why patterns are more than compact syntax: they make the cases you have and have not considered visible.
Use a small table of example inputs when a pattern has alternatives and guards. That makes overlap visible: an axis point that also satisfies a negative-coordinate guard reaches the earlier axis arm. Reorder only when the intended priority is clear.
A pattern describes the shape of a value while optionally giving names to its parts. In match, each arm pairs a pattern with behavior. Matching an enum makes its active variant explicit; matching a tuple can bind two positions at once. A wildcard _ discards a value you do not need. Use it deliberately, because it can also hide a newly added enum variant that deserves handling.
Read one pattern aloud: 'If this is Some(value), bind the contained value and run this arm.' Then trace the alternative None arm. Add a guard only when the variant alone is insufficient, and check whether another arm still covers the remaining values. Try destructuring a struct with two fields and rename one local binding to avoid confusion with the field name. Patterns are a way to state what must be true before a branch runs; the compiler checks the coverage while you decide the behavior.
Try it
Match None and all integer signs, using a guard for negative values. 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 if n % 2 == 0 part do?
Reveal the answer
- It replaces the pattern that precedes it
- Adds a condition the arm must also satisfy
- It makes the match non-exhaustive on its own
A guard is an extra condition; exhaustiveness is still checked across the arms.
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: Pattern Matching (courseId "basics", lessonId "pattern-matching") Link: https://crabrix.com/learn/basics/pattern-matching/ Goal: A guard narrows an arm; the compiler still demands the set be exhaustive. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/pattern-matching) 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.