An enum (short for enumeration) in Rust is a type that can be one of several different variants. It lets you define a type by listing its possible values.
Click to reveal answer
beginner
How does the match statement work with enums in Rust?
The match statement compares a value against patterns. When used with enums, it lets you run different code depending on which variant the enum value is.
Click to reveal answer
intermediate
What happens if you don’t cover all enum variants in a match?
Rust will give a compile-time error because match must be exhaustive. You must handle every possible variant or use a catch-all pattern like _.
Click to reveal answer
intermediate
How can you extract data from enum variants in a match?
If enum variants hold data, you can use pattern matching to bind that data to variables inside the match arms for use.
Click to reveal answer
beginner
Example: What does this code print?<br><pre>enum Color { Red, Green, Blue }<br>let c = Color::Green;<br>match c {<br> Color::Red => println!("Red"),<br> Color::Green => println!("Green"),<br> Color::Blue => println!("Blue"),<br>}</pre>
It prints Green because the variable c is set to Color::Green and the match arm for Color::Green runs.
Click to reveal answer
What keyword is used to compare enum variants in Rust?
Aif
Bmatch
Cswitch
Dcase
✗ Incorrect
Rust uses the match keyword to compare enum variants and run code based on which variant matches.
What must a match statement do when matching an enum?
AHandle all possible variants
BHandle only one variant
CIgnore unmatched variants
DUse a loop
✗ Incorrect
A match must be exhaustive, meaning it must handle all enum variants or use a catch-all pattern.
How do you write a catch-all pattern in a match?
A_
Bdefault
Celse
Dany
✗ Incorrect
The underscore _ is used as a catch-all pattern to match any value not explicitly handled.
If an enum variant holds data, how do you access it in a match?
ABy using a loop
BBy calling a method
CBy casting the enum
DBy binding variables in the pattern
✗ Incorrect
You bind variables in the match pattern to extract data from enum variants.
What will this code print?
enum Direction { Up, Down } let d = Direction::Down; match d { Direction::Up => println!("Up"), Direction::Down => println!("Down"), }
AError
BUp
CDown
DNothing
✗ Incorrect
The variable d is Direction::Down, so the match arm for Down runs and prints "Down".
Explain how the match statement works with enums in Rust.
Think about how you choose actions based on different enum options.
You got /4 concepts.
Describe what happens if you forget to handle an enum variant in a match.
Rust wants you to be sure you covered every case.
You got /3 concepts.
Practice
(1/5)
1.
What does the match statement do when used with enums in Rust?
easy
A. It checks the enum variant and runs the matching code block.
B. It converts the enum into a string automatically.
C. It creates a new enum variant.
D. It deletes the enum variant from memory.
Solution
Step 1: Understand the purpose of match with enums
The match statement compares the enum value against each variant.
Step 2: Identify what happens on a match
When a variant matches, the corresponding code block runs.
Final Answer:
It checks the enum variant and runs the matching code block. -> Option A
Quick Check:
Match runs code by enum variant [OK]
Hint: Match arms correspond to enum variants directly [OK]
Common Mistakes:
Thinking match converts enums to strings
Assuming match creates or deletes variants
Confusing match with if-else statements
2.
Which of the following is the correct syntax to match an enum Color with variants Red, Green, and Blue?
enum Color { Red, Green, Blue }
easy
A. match color { Color.Red => {}, Color.Green => {}, Color.Blue => {} }
B. match color { Red => {}, Green => {}, Blue => {} }
C. match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} }
D. match color { 'Red' => {}, 'Green' => {}, 'Blue' => {} }
Solution
Step 1: Recall enum variant syntax in match
Enum variants must be prefixed with the enum name and double colon, like Color::Red.
Step 2: Check each option's syntax
Only match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} } uses the correct Color::Variant syntax.
Final Answer:
match color { Color::Red => {}, Color::Green => {}, Color::Blue => {} } -> Option C
Quick Check:
Enum variants use EnumName::Variant [OK]
Hint: Use EnumName::Variant in match arms [OK]
Common Mistakes:
Omitting enum name prefix
Using dot instead of double colon
Using strings instead of variants
3.
What will be the output of this Rust code?
enum Direction { North, South, East, West }
fn main() {
let dir = Direction::East;
let message = match dir {
Direction::North => "Going up",
Direction::South => "Going down",
Direction::East => "Going right",
Direction::West => "Going left",
};
println!("{}", message);
}
medium
A. Going right
B. Going up
C. Going left
D. Going down
Solution
Step 1: Identify the enum variant assigned
The variable dir is set to Direction::East.
Step 2: Match dir to the correct message
The match arm for Direction::East returns "Going right".
Final Answer:
Going right -> Option A
Quick Check:
Direction::East matches "Going right" [OK]
Hint: Match returns value for the exact enum variant [OK]
Common Mistakes:
Mixing up enum variants and their messages
Forgetting to use EnumName::Variant in match
Assuming default or fallback arm runs
4.
Find the error in this Rust code snippet using match with enums:
enum Status { Success, Failure }
fn check(status: Status) {
match status {
Status::Success => println!("Success"),
// Missing Failure arm
}
}
medium
A. Missing semicolon after println! macro
B. Function check should return a value
C. Incorrect enum variant syntax
D. Missing match arm for Status::Failure
Solution
Step 1: Check completeness of match arms
The match statement covers Status::Success but misses Status::Failure.