Bird
Raised Fist0
Rustprogramming~20 mins

Match overview in Rust - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Rust Match Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this Rust match expression?
Consider the following Rust code snippet. What will it print when run?
Rust
let number = 3;
match number {
    1 => println!("One"),
    2 | 3 | 5 | 7 | 11 => println!("This is a prime number"),
    13..=19 => println!("A teen number"),
    _ => println!("Something else"),
}
AThis is a prime number
BOne
CA teen number
DSomething else
Attempts:
2 left
๐Ÿ’ก Hint
Look at the pattern that matches the value 3.
โ“ Predict Output
intermediate
2:00remaining
What does this Rust match with tuple print?
Given this Rust code, what is the output?
Rust
let pair = (0, -2);
match pair {
    (0, y) => println!("First is zero and second is {}", y),
    (x, 0) => println!("First is {} and second is zero", x),
    _ => println!("No zeros"),
}
ANo zeros
BFirst is 0 and second is zero
CFirst is zero and second is -2
DFirst is -2 and second is zero
Attempts:
2 left
๐Ÿ’ก Hint
Check which pattern matches the tuple (0, -2).
๐Ÿ”ง Debug
advanced
2:00remaining
What error does this Rust match code produce?
This Rust code snippet tries to match a value but has a problem. What error will the compiler show?
Rust
let x = 5;
match x {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three"),
}
Aerror: non-exhaustive patterns: `4..=i32::MAX` not covered
Berror: mismatched types
CNo error, compiles fine
Derror: expected expression, found keyword `match`
Attempts:
2 left
๐Ÿ’ก Hint
Rust requires match expressions to cover all possible values.
๐Ÿง  Conceptual
advanced
2:00remaining
Which match arm will be chosen for this Rust code?
What will the following Rust code print?
Rust
let value = Some(10);
match value {
    Some(x) if x > 5 => println!("Greater than five: {}", x),
    Some(x) => println!("Less or equal to five: {}", x),
    None => println!("No value"),
}
ACompilation error due to if guard
BLess or equal to five: 10
CNo value
DGreater than five: 10
Attempts:
2 left
๐Ÿ’ก Hint
Check the if guard condition in the first arm.
โ“ Predict Output
expert
3:00remaining
What is the output of this nested match in Rust?
Analyze this Rust code and determine what it prints.
Rust
enum Color {
    Red,
    Green,
    Blue,
}

let color = Color::Green;
match color {
    Color::Red => println!("Red"),
    Color::Green => match color {
        Color::Green => println!("Green inside"),
        _ => println!("Other inside"),
    },
    Color::Blue => println!("Blue"),
}
AGreen
BGreen inside
COther inside
DCompilation error due to unreachable pattern
Attempts:
2 left
๐Ÿ’ก Hint
Look at the nested match inside the Color::Green arm.

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