Bird
Raised Fist0
Rustprogramming~20 mins

Match expression deep dive 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?

Rust
let x = Some(5);
match x {
    Some(n) if n > 3 => println!("Greater than 3: {}", n),
    Some(n) => println!("Some number: {}", n),
    None => println!("No value"),
}
ANo value
BCompilation error
CSome number: 5
DGreater than 3: 5
Attempts:
2 left
💡 Hint

Look at the if guard in the first arm and the value inside Some.

❓ Predict Output
intermediate
2:00remaining
What does this nested match expression print?

Analyze the nested match expression below. What will be printed?

Rust
let value = (2, Some(4));
match value {
    (x, Some(y)) if x == y => println!("Equal: {}" , x),
    (x, Some(y)) => println!("Not equal: {} and {}", x, y),
    _ => println!("No match"),
}
ANot equal: 2 and 4
BEqual: 2
CNo match
DRuntime panic
Attempts:
2 left
💡 Hint

Check the tuple values and the if guard condition.

❓ Predict Output
advanced
2:00remaining
What is the output of this match with shadowing and ref binding?

Consider this Rust code using ref and variable shadowing in a match. What will it print?

Rust
let x = 5;
match x {
    ref r => println!("Got a reference to {}", r),
}
ACompilation error: cannot use ref here
BGot a reference to &5
CGot a reference to 5
DRuntime error
Attempts:
2 left
💡 Hint

Remember that ref creates a reference to the matched value.

❓ Predict Output
advanced
2:00remaining
What is the output of this match with multiple patterns and ranges?

What will this Rust code print?

Rust
let num = 7;
match num {
    1 | 3 | 5 | 7 | 9 => println!("Odd number"),
    2..=8 => println!("Between 2 and 8"),
    _ => println!("Other number"),
}
ACompilation error
BOdd number
COther number
DBetween 2 and 8
Attempts:
2 left
💡 Hint

Check the order of patterns and which one matches first.

❓ Predict Output
expert
3:00remaining
What is the output of this complex match with nested enums and guards?

Given the following Rust enums and match, what will be printed?

Rust
enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

let msg = Message::Move { x: 10, y: 20 };

match msg {
    Message::Move { x, y } if x == y => println!("Move diagonally to ({}, {})", x, y),
    Message::Move { x, y } => println!("Move to ({}, {})", x, y),
    Message::Write(text) => println!("Text message: {}", text),
    Message::Quit => println!("Quit message"),
    _ => println!("Other message"),
}
AMove to (10, 20)
BMove diagonally to (10, 20)
CText message: Move to (10, 20)
DQuit message
Attempts:
2 left
💡 Hint

Look carefully at the if guard condition and the values of x and y.

Practice

(1/5)
1. What does the match expression do in Rust?
easy
A. It loops over a range of numbers.
B. It creates a new variable with a fixed value.
C. It defines a function with multiple parameters.
D. It compares a value against multiple patterns and executes matching code.

Solution

  1. Step 1: Understand the purpose of match

    The match expression is used to compare a value against several patterns.
  2. Step 2: Identify what happens on a match

    When a pattern matches, the corresponding code block runs, making match a clear way to handle multiple cases.
  3. Final Answer:

    It compares a value against multiple patterns and executes matching code. -> Option D
  4. Quick Check:

    Match expression = pattern matching [OK]
Hint: Match compares values to patterns and runs matching code [OK]
Common Mistakes:
  • Thinking match creates variables
  • Confusing match with loops
  • Assuming match defines functions
2. Which of the following is the correct syntax for a simple match expression in Rust?
easy
A. match value { 1 => println!("One") else println!("Other") }
B. match value { 1 => println!("One"), _ => println!("Other") }
C. match value { 1 -> println!("One"), _ -> println!("Other") }
D. match (value) { 1: println!("One"), _ : println!("Other") }

Solution

  1. Step 1: Recall Rust match syntax

    Rust uses curly braces with patterns followed by => and the code to run.
  2. Step 2: Check each option

    match value { 1 => println!("One"), _ => println!("Other") } uses correct syntax with => and a default case using _; others use invalid symbols like : or -> or else.
  3. Final Answer:

    match value { 1 => println!("One"), _ => println!("Other") } -> Option B
  4. Quick Check:

    Correct match syntax uses => and _ for default [OK]
Hint: Use => for patterns and _ for default case in match [OK]
Common Mistakes:
  • Using : or -> instead of =>
  • Omitting commas between arms
  • Using else instead of _ for default
3. What is the output of this Rust code?
let x = Some(5);
match x {
  Some(n) if n > 3 => println!("Greater than 3: {}", n),
  Some(n) => println!("Less or equal to 3: {}", n),
  None => println!("No value"),
}
medium
A. Greater than 3: 5
B. Less or equal to 3: 5
C. No value
D. Compilation error

Solution

  1. Step 1: Analyze the match arms and value

    The value is Some(5). The first arm matches Some(n) with a guard n > 3.
  2. Step 2: Check the guard condition

    Since 5 > 3 is true, the first arm runs and prints "Greater than 3: 5".
  3. Final Answer:

    Greater than 3: 5 -> Option A
  4. Quick Check:

    Guard true triggers first arm output [OK]
Hint: Check if guards are true to pick correct match arm [OK]
Common Mistakes:
  • Ignoring the if guard condition
  • Choosing second arm without checking guard
  • Thinking None matches Some value
4. Identify the error in this Rust match expression:
let num = 2;
match num {
  1 => println!("One"),
  2 => println!("Two")
  3 => println!("Three"),
  _ => println!("Other"),
}
medium
A. Match arms must be in parentheses, not braces.
B. Using _ as default case is invalid.
C. Missing comma after the second arm (2 => ...).
D. Cannot match integer values directly.

Solution

  1. Step 1: Check syntax of each match arm

    Each arm must end with a comma except possibly the last. The second arm lacks a comma after println!("Two").
  2. Step 2: Validate other parts

    Using _ is valid for default, braces are correct, and matching integers is allowed.
  3. Final Answer:

    Missing comma after the second arm (2 => ...). -> Option C
  4. Quick Check:

    Commas separate match arms [OK]
Hint: Check commas after each match arm carefully [OK]
Common Mistakes:
  • Forgetting commas between arms
  • Misunderstanding _ as invalid
  • Confusing braces with parentheses
5. Given a list of Option<i32> values, how can you use a match expression inside a loop to print only the positive numbers, ignoring None and non-positive values? Choose the correct code snippet.
hard
A. for val in list { match val { Some(n) if n > 0 => println!("Positive: {}", n), _ => (), } }
B. for val in list { match val { Some(n) => println!("Positive: {}", n), None => println!("No value"), } }
C. for val in list { if let Some(n) = val { if n > 0 { println!("Positive: {}", n); } } }
D. for val in list { match val { Some(n) if n <= 0 => println!("Positive: {}", n), _ => (), } }

Solution

  1. Step 1: Understand the requirement

    We want to print only positive numbers inside Some, ignoring None and non-positive.
  2. Step 2: Analyze each option

    for val in list { match val { Some(n) if n > 0 => println!("Positive: {}", n), _ => (), } } matches Some(n) with guard n > 0 and prints; others ignore with _ => (). for val in list { match val { Some(n) => println!("Positive: {}", n), None => println!("No value"), } } prints all Some values regardless of positivity. for val in list { if let Some(n) = val { if n > 0 { println!("Positive: {}", n); } } } uses if let but is not a match expression. for val in list { match val { Some(n) if n <= 0 => println!("Positive: {}", n), _ => (), } } prints non-positive numbers, opposite of requirement.
  3. Final Answer:

    for val in list { match val { Some(n) if n > 0 => println!("Positive: {}", n), _ => (), } } -> Option A
  4. Quick Check:

    Match with guard filters positive Some values [OK]
Hint: Use if guards in match to filter values [OK]
Common Mistakes:
  • Printing all Some values without checking positivity
  • Using if let instead of match when asked
  • Reversing guard condition
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