Pattern matching is a way to check a value against a pattern and then execute code based on which pattern matches. It helps handle different cases clearly and safely.
Click to reveal answer
beginner
Why do we need pattern matching instead of simple if-else statements?
Pattern matching lets you handle many different cases in a clean, readable way. It also forces you to cover all possible cases, reducing bugs.
Click to reveal answer
intermediate
How does pattern matching improve code safety?
Rust’s pattern matching checks that all possible cases are handled, so you don’t miss any scenario. This prevents unexpected errors at runtime.
Click to reveal answer
beginner
Give a real-life example where pattern matching is useful.
Imagine sorting mail by type: letters, packages, or magazines. Pattern matching lets you write clear code to handle each type differently, like delivering packages to the door and letters to the mailbox.
Click to reveal answer
intermediate
What Rust feature works closely with pattern matching to handle errors?
The Result type works with pattern matching to handle success or error cases clearly, making error handling safe and easy to read.
Click to reveal answer
What does pattern matching in Rust help you do?
AReplace all if-else statements with loops
BOnly compare numbers
CAutomatically fix bugs
DCheck a value against multiple patterns and run code accordingly
✗ Incorrect
Pattern matching lets you compare a value to many patterns and choose what to do based on the match.
Why is pattern matching safer than using only if-else?
AIt forces you to handle all possible cases
BIt runs faster
CIt uses less memory
DIt hides errors
✗ Incorrect
Rust checks that all cases are covered in pattern matching, preventing missed scenarios.
Which Rust type is commonly used with pattern matching for error handling?
AVec
BOption
CResult
DString
✗ Incorrect
Result is used to represent success or error and is matched to handle each case.
What happens if you don’t cover all cases in a Rust match statement?
AIt runs but crashes
BThe code won’t compile
CIt ignores missing cases
DIt prints a warning
✗ Incorrect
Rust requires all cases to be handled or a default case (_) to compile safely.
Which of these is NOT a benefit of pattern matching?
AAutomatic memory management
BEasier error handling
CCompile-time safety checks
DClear code for many cases
✗ Incorrect
Pattern matching does not manage memory automatically; that is handled by Rust’s ownership system.
Explain why pattern matching is important in Rust and how it helps write safer code.
Think about how Rust forces you to cover all possibilities.
You got /4 concepts.
Describe a simple real-life example where pattern matching would make your code easier to understand.
Imagine sorting mail or handling different types of messages.
You got /3 concepts.
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
Step 1: Understand pattern matching purpose
Pattern matching lets you check different forms of data and handle each case clearly.
Step 2: Compare with other options
The other options describe unrelated or incorrect benefits.
Final Answer:
It helps handle different data cases clearly and safely. -> Option A
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
Step 1: Identify Rust pattern matching syntax
Rust uses the keyword match followed by the value and braces.
Step 2: Eliminate other language syntax
The other options use syntax from other languages or incorrect forms.
Final Answer:
match value { ... } -> Option C
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
Step 1: Analyze the matched value
The variable number is Some(5), so it matches Some(x) with x=5.
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".
Final Answer:
Greater than 3 -> Option B
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
Step 1: Check match arms coverage
The match only covers 1 and 2, but value is 10, so no arm matches.
Step 2: Understand Rust match exhaustiveness
Rust requires all possible cases covered or a catch-all arm like _.
Final Answer:
Missing a catch-all pattern to cover other values -> Option A
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
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.
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.
Final Answer:
match opt {
Some(n) => println!("Number: {}", n),
None => println!("No number"),
} -> Option D
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)