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.
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.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.