0
0
Rustprogramming~10 mins

Match expression deep dive in Rust - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Match expression deep dive
Start with a value
Match expression begins
Check first pattern
NoCheck next pattern
Execute matched arm
Return arm result
End match expression
The match expression checks a value against patterns one by one, executes the first matching arm, then returns its result.
Execution Sample
Rust
let x = 3;
let result = match x {
    1 => "one",
    2 | 3 => "two or three",
    _ => "other",
};
This code matches the value of x to patterns and assigns a string describing it to result.
Execution Table
StepValue of xPattern CheckedMatch?ActionResult
131NoCheck next patternNone
232 | 3YesExecute arm: return "two or three""two or three"
33_ (wildcard)SkippedMatch already found"two or three"
💡 Match found at step 2 with pattern '2 | 3', so match expression ends returning "two or three".
Variable Tracker
VariableStartAfter match
x33
resultuninitialized"two or three"
Key Moments - 3 Insights
Why does the match stop checking after the second pattern even though there is a wildcard pattern after?
Because match expressions stop at the first pattern that matches. In the execution_table row 2, pattern '2 | 3' matches x=3, so the wildcard pattern is skipped (row 3).
What does the underscore (_) pattern mean in a match?
The underscore is a wildcard that matches any value not matched before. It acts like a default case, as shown in execution_table row 3.
Can multiple patterns be combined in one arm?
Yes, using the | operator. In the example, '2 | 3' matches if x is 2 or 3, as seen in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result after step 2?
A"one"
B"two or three"
C"other"
DNone
💡 Hint
Check the 'Result' column at step 2 in the execution_table.
At which step does the match expression stop checking patterns?
AStep 1
BStep 3
CStep 2
DIt checks all patterns
💡 Hint
Look at the 'Match?' column and see where 'Yes' appears first in execution_table.
If x was 4 instead of 3, which pattern would match?
A_ (wildcard)
B2 | 3
C1
DNo pattern matches
💡 Hint
Refer to the wildcard pattern '_' in the execution_table and key_moments about default matching.
Concept Snapshot
match value {
  pattern1 => expr1,
  pattern2 | pattern3 => expr2,
  _ => default_expr,
}
- Checks patterns top to bottom
- Executes first matching arm
- '_' matches anything else
- Arms can combine patterns with '|'
- Returns the arm's expression value
Full Transcript
This visual execution traces a Rust match expression. We start with a value x=3. The match checks patterns in order: first '1' (no match), then '2 | 3' (matches), so it executes that arm returning "two or three". The wildcard '_' pattern is skipped because a match was found. Variables x and result are tracked: x stays 3, result becomes "two or three". Key points: match stops at first match, '_' is a catch-all, and multiple patterns can be combined with '|'. The quiz tests understanding of these steps and outcomes.