Bird
Raised Fist0
Rustprogramming~20 mins

Basic match usage 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 code using match?
Look at this Rust program. What will it print when run?
Rust
fn main() {
    let number = 3;
    match number {
        1 => println!("One"),
        2 => println!("Two"),
        3 => println!("Three"),
        _ => println!("Other"),
    }
}
ATwo
BOther
COne
DThree
Attempts:
2 left
๐Ÿ’ก Hint
Check which pattern matches the value 3 in the match expression.
โ“ Predict Output
intermediate
2:00remaining
What does this Rust match expression return?
What value does this Rust code assign to 'result'?
Rust
fn main() {
    let x = 5;
    let result = match x {
        1..=4 => "small",
        5..=10 => "medium",
        _ => "large",
    };
    println!("{}", result);
}
Amedium
Bsmall
Clarge
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Look at the range patterns and see where 5 fits.
โ“ Predict Output
advanced
2:00remaining
What is the output of this nested match in Rust?
Analyze the nested match statements and find the output.
Rust
fn main() {
    let a = Some(2);
    let b = 3;
    match a {
        Some(x) => match b {
            3 => println!("x is {} and b is three", x),
            _ => println!("x is {} and b is not three", x),
        },
        None => println!("No value for a"),
    }
}
ANo value for a
Bx is 2 and b is three
Cx is 2 and b is not three
DCompilation error
Attempts:
2 left
๐Ÿ’ก Hint
Check the values inside the nested match carefully.
โ“ Predict Output
advanced
2:00remaining
What error does this Rust code produce?
What error will this Rust code cause when compiled?
Rust
fn main() {
    let num = 7;
    match num {
        1 => println!("One"),
        2 => println!("Two"),
        _ => (),
    }
}
AError: variable 'num' not found
BError: mismatched types
CError: non-exhaustive patterns in match
DNo error, prints nothing
Attempts:
2 left
๐Ÿ’ก Hint
Think about what happens if 'num' is 7 and no match arm covers it.
๐Ÿง  Conceptual
expert
2:00remaining
How many arms does this Rust match expression have?
Count the number of arms in this Rust match expression.
Rust
fn main() {
    let val = 10;
    match val {
        1 | 2 | 3 => println!("One, two or three"),
        4..=10 => println!("Between four and ten"),
        _ => println!("Something else"),
    }
}
A3
B2
C4
D5
Attempts:
2 left
๐Ÿ’ก Hint
Count each separate match arm, not the number of values in patterns.

Practice

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

Solution

  1. Step 1: Understand the purpose of match

    The match expression compares a value against several patterns.
  2. Step 2: Identify what happens on a match

    When a pattern matches, the corresponding code block runs.
  3. Final Answer:

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

    match checks patterns = C [OK]
Hint: Remember: match checks value patterns and runs matching code [OK]
Common Mistakes:
  • Thinking match creates variables
  • Confusing match with loops
  • Believing match defines functions
2. Which of the following is the correct syntax for a match expression in Rust?
easy
A. match value { pattern : action, _ : default_action }
B. match (value) { pattern -> action, _ -> default_action }
C. match value { pattern => action, _ => default_action }
D. match value (pattern => action, _ => default_action)

Solution

  1. Step 1: Recall Rust match syntax

    Rust uses curly braces with patterns and arrows: { pattern => action }.
  2. Step 2: Check each option's syntax

    match value { pattern => action, _ => default_action } matches Rust syntax exactly; others use wrong symbols or parentheses.
  3. Final Answer:

    match value { pattern => action, _ => default_action } -> Option C
  4. Quick Check:

    Correct match syntax = A [OK]
Hint: Look for curly braces and => arrows in match syntax [OK]
Common Mistakes:
  • Using parentheses instead of braces
  • Using -> instead of =>
  • Using colons instead of arrows
3. What is 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.
  2. Step 2: Find matching pattern in match arms

    The pattern 3 matches, so it prints "Three".
  3. Final Answer:

    Three -> Option D
  4. Quick Check:

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

Solution

  1. Step 1: Check match arms coverage

    The match covers only 1, 2, and 3 but variable is 5.
  2. Step 2: Identify missing default arm

    Without a _ arm, the code will not compile because not all values are covered.
  3. Final Answer:

    Missing default (_) arm to cover other values -> Option B
  4. Quick Check:

    All values must be matched or _ arm added [OK]
Hint: Always add _ arm to cover unmatched cases [OK]
Common Mistakes:
  • Assuming semicolons are required after println!
  • Ignoring missing default arm errors
  • Confusing variable names
5. You want to write a match expression that returns a string describing a number: "small" for 1 or 2, "medium" for 3 or 4, and "large" for any other number. Which code correctly uses match to do this?
hard
A. match num { 1 | 2 => "small", 3 | 4 => "medium", _ => "large", }
B. match num { 1, 2 => "small", 3, 4 => "medium", _ => "large", }
C. match num { 1 || 2 => "small", 3 || 4 => "medium", _ => "large", }
D. match num { [1, 2] => "small", [3, 4] => "medium", _ => "large", }

Solution

  1. Step 1: Understand pattern alternatives in match

    Rust uses | to match multiple patterns in one arm.
  2. Step 2: Check each option's pattern syntax

    match num { 1 | 2 => "small", 3 | 4 => "medium", _ => "large", } correctly uses 1 | 2 and 3 | 4. Others use invalid syntax like commas, ||, or arrays.
  3. Final Answer:

    match num { 1 | 2 => "small", 3 | 4 => "medium", _ => "large", } -> Option A
  4. Quick Check:

    Use | for multiple patterns in match arms [OK]
Hint: Use | to combine multiple patterns in one match arm [OK]
Common Mistakes:
  • Using commas instead of | in patterns
  • Using || which is logical OR, not pattern OR
  • Trying to match arrays instead of values
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