Enums & Pattern Matching Questions with Answers

17 practice questions on Enums & Pattern Matching (Rust) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Easy

What does the matches!(x, Some(_)) macro return?

  1. The inner value of x, unwrapped for you
  2. An Option wrapping the match result
  3. A bool that is true when x is a Some
  4. 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

  1. Takes value and pattern.
  2. Tests the pattern.
  3. 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

Rust enums can:

  1. Only have two variants
  2. Inherit fields from a parent enum, like classes
  3. Only be named integer constants
  4. 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

  1. Each enum variant can carry its own data.
  2. For example, Circle holds a radius and Rect holds width and height.
  3. 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?

  1. Compile error: non-exhaustive patterns
  2. It compiles and ignores the variant
  3. A run-time panic only when that variant occurs
  4. 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

  1. match covers all values.
  2. A variant is missing.
  3. 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);
  1. 12
  2. 6
  3. 5
  4. 27

Answer: B. 6

Explanation

The value is Rect(2, 3), so the second arm computes 2 * 3 = 6.

Step-by-step solution

  1. s is Rect.
  2. w=2, h=3.
  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:

  1. Unit-like, struct-like and tuple-like respectively
  2. Separate structs that must be declared elsewhere
  3. Integer constants 0, 1 and 2 only
  4. 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

  1. Quit has no data: unit-like.
  2. Move {x, y} has named fields: struct-like.
  3. 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

What is a match guard?

  1. An extra if condition on an arm, e.g. Some(x) if x > 5
  2. A lifetime annotation
  3. A default arm
  4. 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

  1. A match guard is an if condition added after a pattern.
  2. For example, Some(x) if x greater than 5 matches only when the guard is true.
  3. 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?

  1. Binds the value to a variable named _
  2. Matches only the unit value ()
  3. Matches any value not covered by earlier arms
  4. 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

  1. The underscore pattern matches any value.
  2. It is used as a catch-all for cases no earlier arm covers.
  3. 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?

  1. Pushes items until the stack is full
  2. Loops forever
  3. Runs once
  4. 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

  1. While let repeats while the pattern matches.
  2. Pop returns Some while items remain and None when the stack is empty.
  3. 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)?

  1. 0
  2. It panics
  3. Compile error: else needs a value
  4. -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

  1. o is None.
  2. Pattern Some(v) fails.
  3. 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:

  1. Strings cannot be compared
  2. Enums can store any string value at runtime
  3. Invalid states cannot be represented, and matches are checked
  4. 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

  1. An enum has a fixed set of variants.
  2. A typo in a variant name is a compile error, and a match must cover every variant.
  3. 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 →

Related topics

See all Rust topics · All topics