Sample questions
Question 1Easy
What does the matches!(x, Some(_)) macro return?
- The inner value of x, unwrapped for you
- An Option wrapping the match result
- A bool that is true when x is a Some
- It panics when x is None
Answer: C. A bool that is true when x is a Some
Explanation
matches! evaluates a pattern against a value and returns true or false.
Step-by-step solution
- Takes value and pattern.
- Tests the pattern.
- Returns bool.
Why the other options are wrong
- A. It does not extract values.
- B. It returns a plain bool.
- D. It never panics.
Question 2Easy
- Only have two variants
- Inherit fields from a parent enum, like classes
- Only be named integer constants
- Hold data in each variant
Answer: D. Hold data in each variant
Explanation
enum Shape { Circle(f64), Rect { w: f64, h: f64 } } carries different data per variant.
Step-by-step solution
- Each enum variant can carry its own data.
- For example, Circle holds a radius and Rect holds width and height.
- So option D is the correct answer.
Why the other options are wrong
- A. Enums can have any number of variants.
- B. Enums do not inherit fields from a parent.
- C. Enum variants can hold data, not only integer constants.
Tip: Use enums with data to model a value that can take one of several shapes.
Question 3Easy
What happens if a match on an enum leaves one variant unhandled and has no _ arm?
- Compile error: non-exhaustive patterns
- It compiles and ignores the variant
- A run-time panic only when that variant occurs
- A warning only
Answer: A. Compile error: non-exhaustive patterns
Explanation
match must be exhaustive. The compiler lists the missing variants so no case is forgotten.
Step-by-step solution
- match covers all values.
- A variant is missing.
- Compilation fails.
Why the other options are wrong
- B. Rust forces every case to be covered.
- C. Detection happens at compile time.
- D. It is a hard error.
Question 4Easy
What does this print?
enum Shape { Circle(i32), Rect(i32, i32) }
let s = Shape::Rect(2, 3);
let a = match s {
Shape::Circle(r) => 3 * r * r,
Shape::Rect(w, h) => w * h,
};
println!("{}", a);
- 12
- 6
- 5
- 27
Answer: B. 6
Explanation
The value is Rect(2, 3), so the second arm computes 2 * 3 = 6.
Step-by-step solution
- s is Rect.
- w=2, h=3.
- w * h = 6.
Why the other options are wrong
- A. That would need different values.
- C. 5 is w + h, not the product.
- D. 27 is for Circle(3).
Question 5Medium
An enum Message { Quit, Move { x: i32, y: i32 }, Write(String) } has variants that are:
- Unit-like, struct-like and tuple-like respectively
- Separate structs that must be declared elsewhere
- Integer constants 0, 1 and 2 only
- All required to hold the same data type
Answer: A. Unit-like, struct-like and tuple-like respectively
Explanation
Each variant can carry no data, named fields, or positional fields.
Step-by-step solution
- Quit has no data: unit-like.
- Move {x, y} has named fields: struct-like.
- Write(String) has positional field: tuple-like.
Why the other options are wrong
- B. Variants are declared inside the enum.
- C. Variants hold data here.
- D. Variants may hold different data.
Question 6Medium
- An extra if condition on an arm, e.g. Some(x) if x > 5
- A lifetime annotation
- A default arm
- A trait that prevents a match from panicking
Answer: A. An extra if condition on an arm, e.g. Some(x) if x > 5
Explanation
The arm matches only if the pattern fits and the guard is true.
Step-by-step solution
- A match guard is an if condition added after a pattern.
- For example, Some(x) if x greater than 5 matches only when the guard is true.
- So option A is the correct answer.
Why the other options are wrong
- B. A lifetime annotation is a separate syntax, not a guard.
- C. A default arm is an underscore or name pattern, not a guard.
- D. No trait stops a match from panicking; a guard is a condition.
Tip: Use a guard when a pattern alone cannot express the condition.
Question 7Medium
What does the _ pattern do in a match?
- Binds the value to a variable named _
- Matches only the unit value ()
- Matches any value not covered by earlier arms
- Matches nothing
Answer: C. Matches any value not covered by earlier arms
Explanation
_ is a catch-all wildcard that does not bind.
Step-by-step solution
- The underscore pattern matches any value.
- It is used as a catch-all for cases no earlier arm covers.
- So option C is the correct answer.
Why the other options are wrong
- A. _ does not bind the value to a name.
- B. _ matches every value, not only the unit value.
- D. _ matches values, so it does not match nothing.
Tip: Put the _ arm last, since it catches everything left over.
Question 8Medium
What does while let Some(top) = stack.pop() { ... } do?
- Pushes items until the stack is full
- Loops forever
- Runs once
- Loops, popping until pop returns None
Answer: D. Loops, popping until pop returns None
Explanation
while let repeats while the pattern matches.
Step-by-step solution
- While let repeats while the pattern matches.
- Pop returns Some while items remain and None when the stack is empty.
- So the loop pops until pop returns None; option D is the correct answer.
Why the other options are wrong
- A. The loop pops items; it does not push them.
- B. The loop stops when pop returns None, so it does not loop forever.
- C. The loop repeats while items remain, not just once.
Tip: While let Some(item) = stack.pop() is a neat way to drain a stack.
Question 9Hard
What does this let ... else do?
fn f(o: Option<i32>) -> i32 {
let Some(v) = o else { return -1 };
v + 1
}
What is f(None)?
- 0
- It panics
- Compile error: else needs a value
- -1
Answer: D. -1
Explanation
If the pattern fails, the else block runs and must diverge (return, break, panic). Here it returns -1.
Step-by-step solution
- o is None.
- Pattern Some(v) fails.
- else returns -1.
Why the other options are wrong
- A. The else block returns -1, not 0.
- B. let-else does not panic.
- C. The else block must diverge, and return does.
Question 10Hard
Modelling a traffic light as enum Light { Red, Yellow, Green } instead of strings mainly helps because:
- Strings cannot be compared
- Enums can store any string value at runtime
- Invalid states cannot be represented, and matches are checked
- Enums are faster to print
Answer: C. Invalid states cannot be represented, and matches are checked
Explanation
'Make illegal states unrepresentable': typos and missing cases become compile errors.
Step-by-step solution
- An enum has a fixed set of variants.
- A typo in a variant name is a compile error, and a match must cover every variant.
- So invalid states cannot be represented; option C is the correct answer.
Why the other options are wrong
- A. Strings can be compared; the problem is typos and invalid values.
- B. An enum holds only its declared variants, not any string.
- D. Printing speed is not the reason to use an enum.
Tip: Model a fixed set of states as an enum, so illegal values cannot be written.
Practise all 17 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →