Bird
Raised Fist0
Rustprogramming~10 mins

Match overview in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Match overview
Start: Have a value
↓
Match value against patterns
↓
Check pattern 1
No→Check pattern 2
↓
Execute code block
↓
End
The match expression checks a value against multiple patterns one by one and runs the code for the first matching pattern.
Execution Sample
Rust
let number = 2;
match number {
    1 => println!("One"),
    2 => println!("Two"),
    _ => println!("Other"),
}
This code matches the variable 'number' against patterns 1, 2, and a default, printing the matching case.
Execution Table
StepMatched ValuePattern CheckedMatch ResultAction TakenOutput
121NoCheck next pattern
222YesExecute println!("Two")Two
32_ (default)SkippedNo action
42EndMatch found, exitStop matching
💡 Match found at pattern 2, so matching stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
number2222
Key Moments - 2 Insights
Why does the match stop checking after finding the first match?
Because Rust's match expression runs only the first matching pattern's code and then exits, as shown in execution_table step 4.
What does the underscore (_) pattern mean in a match?
It means 'any other value' and acts as a default case if no previous patterns match, as seen in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what pattern matches the value 2?
APattern 1
BDefault (_) pattern
CPattern 2
DNo pattern matches
💡 Hint
Check the 'Match Result' column in execution_table row 2.
At which step does the match expression stop checking patterns?
AStep 4
BStep 2
CStep 1
DStep 3
💡 Hint
Look at the 'Action Taken' and 'Match Result' columns in execution_table step 4.
If number was 5, which pattern would match?
APattern 2
BDefault (_) pattern
CPattern 1
DNo pattern matches
💡 Hint
The default pattern matches any value not matched before, as explained in key_moments.
Concept Snapshot
match value {
    pattern1 => action1,
    pattern2 => action2,
    _ => default_action,
}

- Checks patterns top to bottom
- Runs first matching action
- '_' is default catch-all
- Stops after first match
Full Transcript
The Rust match expression compares a value against multiple patterns in order. It starts with the first pattern and checks if the value fits. If it does, it runs the code for that pattern and stops checking further. If not, it moves to the next pattern. The underscore (_) pattern is a default that matches anything not matched before. This way, match expressions let you run different code depending on the value, similar to a switch-case but more powerful and safe.

Practice

(1/5)
1. What is the main purpose of the match statement in Rust?
easy
A. To check a value against multiple patterns clearly and concisely
B. To create loops that run a fixed number of times
C. To define new data types
D. To handle errors using exceptions

Solution

  1. Step 1: Understand the role of match

    The match statement compares a value against many patterns, making code clearer than many if-else statements.
  2. Step 2: Compare with other options

    Loops run repeatedly, data types define structures, and exceptions handle errors, but match is for pattern matching.
  3. Final Answer:

    To check a value against multiple patterns clearly and concisely -> Option A
  4. Quick Check:

    Match = pattern checking [OK]
Hint: Match replaces many if-else with clear pattern checks [OK]
Common Mistakes:
  • Confusing match with loops
  • Thinking match defines types
  • Assuming match handles exceptions
2. Which of the following is the correct syntax for a match expression in Rust?
easy
A. match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr }
B. match (value) { pattern1: expr1; pattern2: expr2; default: default_expr }
C. match value { pattern1 -> expr1, pattern2 -> expr2, _ -> default_expr }
D. match value { pattern1 => expr1; pattern2 => expr2; _ => default_expr }

Solution

  1. Step 1: Recall Rust match syntax

    Rust uses curly braces with patterns followed by => and expressions separated by commas.
  2. Step 2: Check each option

    match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } uses correct braces, arrows, and commas. Options B and D use semicolons incorrectly. match value { pattern1 -> expr1, pattern2 -> expr2, _ -> default_expr } uses -> instead of =>.
  3. Final Answer:

    match value { pattern1 => expr1, pattern2 => expr2, _ => default_expr } -> Option A
  4. Quick Check:

    Correct match syntax uses => and commas [OK]
Hint: Use => between pattern and expression, separate arms with commas [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Using -> instead of =>
  • Missing the catch-all _ pattern
3. What will be the output of this Rust code?
let number = 3;
match number {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three"),
    _ => println!("Other"),
}
medium
A. One
B. Two
C. Other
D. Three

Solution

  1. Step 1: Identify the matched value

    The variable number is 3, so the match will look for pattern 3.
  2. Step 2: Find the matching arm

    The arm 3 => println!("Three") matches and prints "Three".
  3. Final Answer:

    Three -> Option D
  4. Quick Check:

    number=3 matches 3 => "Three" [OK]
Hint: Match value exactly to print correct output [OK]
Common Mistakes:
  • Choosing default _ arm instead of exact match
  • Confusing numbers with strings
  • Ignoring the value of number
4. Identify the error in this Rust match code snippet:
let x = 5;
match x {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three")
}
medium
A. Missing semicolon after println! macros
B. Using parentheses instead of curly braces in match
C. Missing catch-all _ pattern to handle other values
D. Incorrect variable declaration

Solution

  1. Step 1: Check match arms completeness

    The match covers 1, 2, and 3 but does not handle other values like 5.
  2. Step 2: Understand Rust match requirements

    Rust requires all possible values to be covered, usually by a catch-all _ pattern.
  3. Final Answer:

    Missing catch-all _ pattern to handle other values -> Option C
  4. Quick Check:

    Always include _ to cover unmatched cases [OK]
Hint: Always add _ arm to handle unexpected values [OK]
Common Mistakes:
  • Forgetting the catch-all _ pattern
  • Thinking semicolons are mandatory after println!
  • Confusing braces with parentheses
5. Given this Rust code, what will be the output?
let grade = 'B';
match grade {
    'A' | 'B' | 'C' => println!("Pass"),
    'D' => println!("Conditional Pass"),
    _ => println!("Fail"),
}
hard
A. Conditional Pass
B. Pass
C. Fail
D. Compilation error

Solution

  1. Step 1: Identify the value and patterns

    The variable grade is 'B'. The first arm matches 'A', 'B', or 'C'.
  2. Step 2: Determine which arm runs

    Since 'B' matches the first arm, it prints "Pass".
  3. Final Answer:

    Pass -> Option B
  4. Quick Check:

    Multiple patterns with | match 'B' => "Pass" [OK]
Hint: Use | to match multiple patterns in one arm [OK]
Common Mistakes:
  • Assuming only single pattern per arm
  • Choosing wrong output for 'B'
  • Thinking code causes compilation error
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard