Recall & Review
beginner
What is a
match expression in Rust?A
match expression lets you compare a value against many patterns and execute code based on which pattern matches. It's like a smart switch that checks all possibilities.Click to reveal answer
beginner
How does Rust handle unmatched patterns in a
match expression?Rust requires all possible cases to be covered in a
match. If some cases are missing, the code won't compile. You can use a catch-all pattern _ to cover any leftover cases.Click to reveal answer
intermediate
What is the role of the
@ operator in a match pattern?The
@ operator lets you name a value while also testing it against a pattern. For example, id @ 1..=5 means "match values from 1 to 5 and call it id".Click to reveal answer
beginner
Explain how to match multiple patterns in one arm of a
match expression.You can match multiple patterns separated by
| in one arm. For example, 1 | 2 | 3 => println!("One to three") runs the same code for 1, 2, or 3.Click to reveal answer
beginner
What happens if you forget to handle a pattern in a
match expression?Rust will give a compile-time error saying the match is not exhaustive. This helps avoid bugs by forcing you to handle all possible cases.
Click to reveal answer
What does the underscore
_ pattern do in a match expression?✗ Incorrect
The underscore
_ is a catch-all pattern that matches any value not matched before.How do you bind a matched value to a variable inside a
match arm?✗ Incorrect
The
@ operator lets you name the matched value while testing it against a pattern.Which of these is a valid way to match multiple values in one
match arm?✗ Incorrect
The pipe
| operator matches any one of the listed patterns.What will happen if a
match expression does not cover all possible cases?✗ Incorrect
Rust enforces exhaustive matching at compile time to prevent bugs.
In a
match expression, what does this arm do? Some(x) => println!("Value: {}", x)✗ Incorrect
This matches the Some variant of Option and binds the inner value to x.
Explain how Rust's
match expression ensures all cases are handled and why this is helpful.Think about what happens if you forget a case.
You got /4 concepts.
Describe how to use the
@ operator in a match pattern and give an example.It lets you name the value while matching it.
You got /3 concepts.