Bird
Raised Fist0
Rustprogramming~20 mins

Why pattern matching is needed in Rust - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pattern Matching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use pattern matching instead of if-else chains?

In Rust, pattern matching is often preferred over long if-else chains. Why is this the case?

APattern matching allows checking multiple conditions in a clear, concise way and ensures all cases are handled.
BPattern matching is slower but easier to write than if-else chains.
CIf-else chains can only check one condition at a time, pattern matching can check none.
DPattern matching is only used for error handling, not for general conditions.
Attempts:
2 left
💡 Hint

Think about how pattern matching helps with clarity and completeness.

❓ Predict Output
intermediate
2:00remaining
What is the output of this Rust code using pattern matching?

Consider this Rust code snippet:

let number = 3;
match number {
    1 => println!("One"),
    2 => println!("Two"),
    3 => println!("Three"),
    _ => println!("Other"),
}

What will it print?

AOther
BTwo
COne
DThree
Attempts:
2 left
💡 Hint

Look at the value of number and which arm matches it.

🔧 Debug
advanced
2:00remaining
What error does this Rust pattern matching code produce?

Look at this Rust code:

let value = Some(5);
match value {
    Some(x) => println!("Value is {}", x),
}

What error will the compiler show?

AError: match arms have different types
BError: non-exhaustive patterns, missing None case
CError: variable x not found
DNo error, code compiles fine
Attempts:
2 left
💡 Hint

Think about whether all possible cases of Option are covered.

📝 Syntax
advanced
2:00remaining
Which option correctly uses pattern matching with guards in Rust?

Which of these Rust code snippets correctly uses a pattern guard in a match arm?

A
match x {
    n > 0 => println!("Positive"),
    _ => println!("Non-positive"),
}
B
match x {
    n if n > 0: println!("Positive"),
    _ => println!("Non-positive"),
}
C
match x {
    n if n > 0 => println!("Positive"),
    _ => println!("Non-positive"),
}
D
match x {
    n if (n > 0) => println!("Positive"),
    _ => println!("Non-positive"),
}
Attempts:
2 left
💡 Hint

Remember the syntax for pattern guards uses if after the pattern.

🚀 Application
expert
2:00remaining
How many arms are needed to exhaustively match this enum?

Given this Rust enum:

enum TrafficLight {
    Red,
    Yellow,
    Green,
}

How many arms are needed in a match statement to cover all cases without using a wildcard arm?

A3
B2
C1
D4
Attempts:
2 left
💡 Hint

Count the number of variants in the enum.

Practice

(1/5)
1. Why is pattern matching important in Rust programming?
easy
A. It helps handle different data cases clearly and safely.
B. It makes the program run faster than loops.
C. It automatically fixes syntax errors.
D. It replaces all functions with simpler code.

Solution

  1. Step 1: Understand pattern matching purpose

    Pattern matching lets you check different forms of data and handle each case clearly.
  2. Step 2: Compare with other options

    The other options describe unrelated or incorrect benefits.
  3. Final Answer:

    It helps handle different data cases clearly and safely. -> Option A
  4. Quick Check:

    Pattern matching = clear, safe data handling [OK]
Hint: Pattern matching handles many cases clearly and safely [OK]
Common Mistakes:
  • Thinking pattern matching speeds up code execution
  • Believing it fixes syntax errors automatically
  • Confusing pattern matching with function replacement
2. Which of the following is the correct syntax to start a pattern match in Rust?
easy
A. switch(value) { ... }
B. if value == { ... }
C. match value { ... }
D. case value: { ... }

Solution

  1. Step 1: Identify Rust pattern matching syntax

    Rust uses the keyword match followed by the value and braces.
  2. Step 2: Eliminate other language syntax

    The other options use syntax from other languages or incorrect forms.
  3. Final Answer:

    match value { ... } -> Option C
  4. Quick Check:

    Rust pattern match starts with 'match' [OK]
Hint: Rust uses 'match' keyword for pattern matching [OK]
Common Mistakes:
  • Using 'switch' instead of 'match'
  • Trying to use 'case' like in other languages
  • Confusing 'if' statement with pattern matching
3. What will this Rust code print?
let number = Some(5);
match number {
    Some(x) if x > 3 => println!("Greater than 3"),
    Some(_) => println!("3 or less"),
    None => println!("No value"),
}
medium
A. 3 or less
B. Greater than 3
C. No value
D. Compilation error

Solution

  1. Step 1: Analyze the matched value

    The variable number is Some(5), so it matches Some(x) with x=5.
  2. Step 2: Check the guard condition

    The first arm has a condition if x > 3, which is true for 5, so it prints "Greater than 3".
  3. Final Answer:

    Greater than 3 -> Option B
  4. Quick Check:

    Some(5) with x>3 prints 'Greater than 3' [OK]
Hint: Check guard conditions in match arms carefully [OK]
Common Mistakes:
  • Ignoring the if guard and picking second arm
  • Thinking None matches Some(5)
  • Assuming compilation error due to guard
4. Identify the error in this Rust code using pattern matching:
let value = 10;
match value {
    1 => println!("One"),
    2 => println!("Two"),
}
medium
A. Missing a catch-all pattern to cover other values
B. Incorrect use of println! macro
C. Variable 'value' should be mutable
D. Match arms must return a value

Solution

  1. Step 1: Check match arms coverage

    The match only covers 1 and 2, but value is 10, so no arm matches.
  2. Step 2: Understand Rust match exhaustiveness

    Rust requires all possible cases covered or a catch-all arm like _.
  3. Final Answer:

    Missing a catch-all pattern to cover other values -> Option A
  4. Quick Check:

    Match must cover all cases or use catch-all [OK]
Hint: Always include a catch-all (_) arm if not all cases covered [OK]
Common Mistakes:
  • Forgetting match must be exhaustive
  • Thinking println! usage is wrong
  • Assuming variable must be mutable
5. You want to safely extract a number from an Option<i32> and print it, or print "No number" if None. Which pattern matching code correctly does this?
hard
A. match opt { Some(n) if n > 0 => println!("Number: {}", n), _ => println!("No number"), }
B. if let Some(n) = opt { println!("Number: {}", n); } else { println!("No number"); }
C. match opt { Some => println!("Number: {}", opt), None => println!("No number"), }
D. match opt { Some(n) => println!("Number: {}", n), None => println!("No number"), }

Solution

  1. Step 1: Check correct pattern matching syntax

    match opt { Some(n) => println!("Number: {}", n), None => println!("No number"), } matches Some(n) and None correctly and prints accordingly.
  2. Step 2: Evaluate other options

    The option with a guard condition if n > 0 incorrectly excludes non-positive numbers. The if let approach works but uses destructuring instead of a full match. The option with Some => lacks variable binding and would print the Option rather than the number.
  3. Final Answer:

    match opt { Some(n) => println!("Number: {}", n), None => println!("No number"), } -> Option D
  4. Quick Check:

    Correct pattern match covers Some and None [OK]
Hint: Match Some(n) and None explicitly for safe extraction [OK]
Common Mistakes:
  • Using if let instead of match (not pattern matching)
  • Missing variable in Some pattern
  • Adding unnecessary conditions excluding valid 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