Rust Enums Quiz
40 comprehensive questions on Rust's enum system, covering simple variants, data-carrying variants, enum matching, methods on enums, and Option/Result patterns — with 11 code examples demonstrating practical enum usage.
Question 1
What is an enum in Rust and why are they useful?
Question 2
How do you define a simple enum with unit variants in Rust?
Question 3
What are data-carrying variants in enums?
Question 4
How do you define an enum variant that carries a single value?
Question 5
What will this enum definition create?
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}Question 6
How do you create instances of enum variants?
Question 7
What is the key benefit of using enums over constants or integers for representing different states?
Question 8
How do you implement methods on enums?
Question 9
What will this enum method implementation do?
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
impl Coin {
fn value_in_cents(&self) -> u32 {
match self {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
}Question 10
What is the Option enum and why is it commonly used?
Question 11
How is the Option enum defined in Rust's standard library?
Question 12
What is the Result enum used for?
Question 13
How do you handle Option values in practice?
Question 14
What will this Option handling code output?
fn main() {
let some_number = Some(42);
let absent_number: Option<i32> = None;
match some_number {
Some(value) => println!("Got a value: {}", value),
None => println!("No value"),
}
match absent_number {
Some(value) => println!("Got a value: {}", value),
None => println!("No value"),
}
}Question 15
How do you use if let for concise Option handling?
Question 16
What is the ? operator used for with Result?
Question 17
In a scenario where you're implementing a function that might fail, how should you design its return type?
Question 18
How do you implement the Display trait for an enum?
Question 19
What is enum variant visibility and how does it work?
Question 20
How do you create associated functions (static methods) on enums?
Question 21
What will this associated function on an enum create?
enum Color {
Red,
Green,
Blue,
}
impl Color {
fn from_rgb(r: u8, g: u8, b: u8) -> Option<Self> {
match (r, g, b) {
(255, 0, 0) => Some(Color::Red),
(0, 255, 0) => Some(Color::Green),
(0, 0, 255) => Some(Color::Blue),
_ => None,
}
}
}Question 22
How do you handle Result values in a match expression?
Question 23
What is the difference between Option and Result enums?
Question 24
How do you chain operations that return Result using the ? operator?
Question 25
In a scenario where you're parsing user input that might be invalid, how should you structure the parsing function?
Question 26
What is enum discriminant and how does it work?
Question 27
How do you customize enum discriminants?
Question 28
What is the memory layout of enums in Rust?
Question 29
How do you implement From or Into traits for enums?
Question 30
What will this enum conversion implementation do?
enum Status {
Active,
Inactive,
}
impl From<bool> for Status {
fn from(b: bool) -> Self {
if b { Status::Active } else { Status::Inactive }
}
}Question 31
How do you handle C-style enums with custom discriminants?
Question 32
What is the difference between tuple variants and struct variants in enums?
Question 33
How do you pattern match on struct variants?
Question 34
What will this struct variant matching code do?
enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
fn main() {
let shape = Shape::Rectangle { width: 10.0, height: 20.0 };
match shape {
Shape::Circle { radius } => println!("Circle with radius {}", radius),
Shape::Rectangle { width, height } => println!("Rectangle {}x{}", width, height),
}
}Question 35
How do you implement Default trait for an enum?
Question 36
What is the advantage of using enums over inheritance hierarchies?
Question 37
How do you handle nested enums in pattern matching?
Question 38
What will this nested enum matching demonstrate?
enum Result {
Ok(i32),
Err(String),
}
fn main() {
let result = Result::Ok(42);
match result {
Result::Ok(n) if n > 0 => println!("Positive number: {}", n),
Result::Ok(n) => println!("Zero or negative: {}", n),
Result::Err(msg) => println!("Error: {}", msg),
}
}Question 39
How do you create recursive enums (enums that contain themselves)?
Question 40
In a complex application where you need to represent different types of user interface events, how would you design the event enum?
