What is the difference between a struct and an enum in Rust?
Learn the difference between structs and enums in Rust: product types versus sum types, variant data, and exhaustive pattern matching, with clear examples.
Expected Interview Answer
A struct groups several related values together that all exist at once (an AND of fields), while an enum defines a type that is exactly one of several possible variants at a time (an OR of choices).
A struct is a product type: a Point holds an x and a y simultaneously. An enum is a sum type: a Shape is a Circle or a Rectangle or a Triangle, never more than one. Enum variants can each carry their own data, and the compiler forces you to handle every variant with pattern matching, which makes illegal states unrepresentable.
- Structs model 'has-a' data with all fields present together
- Enums model 'is-one-of' state with mutually exclusive variants
- Enum variants can hold different shapes of data
- match forces exhaustive handling of every enum case
- Both support methods via impl blocks
AI Mentor Explanation
A struct is a batter's full profile card: name, team, and average all filled in together at once. An enum is the result of a single delivery, which is exactly one outcome — a dot, a single, a boundary, or a wicket — never two at the same moment, and each outcome can carry its own extra data like how many runs.
Step-by-Step Explanation
Step 1
Reach for a struct
Use a struct when you need several values that all belong together and are all present at the same time.
Step 2
Reach for an enum
Use an enum when a value is exactly one of a fixed set of alternatives that never overlap.
Step 3
Attach data to variants
Give enum variants their own fields so each case carries only the data that case needs.
Step 4
Add behavior
Write impl blocks on either to attach methods; enums often centralize logic in a single match.
Step 5
Handle every case
Use match on the enum so the compiler forces you to cover all variants, preventing forgotten states.
What Interviewer Expects
- Product type versus sum type understanding
- Knowing enum variants can carry data
- Awareness of exhaustive pattern matching
- A clear real-world example of each
- Mentioning 'make illegal states unrepresentable'
Common Mistakes
- Thinking Rust enums are just integer constants like in C
- Not realizing enum variants can hold their own fields
- Using a struct with many bool flags where an enum fits better
- Forgetting that match must be exhaustive
Best Answer (HR Friendly)
“A struct bundles several pieces of information that all belong together, like a form with name, age, and address filled in at once. An enum represents something that is only one of a few options at a time, like a traffic light being red, yellow, or green but never two at once.”
Code Example
struct Point {
x: f64,
y: f64,
}
enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
impl Shape {
fn area(&self) -> f64 {
match self {
Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
Shape::Rectangle { width, height } => width * height,
}
}
}
fn main() {
let center = Point { x: 0.0, y: 0.0 };
let s = Shape::Circle { radius: 2.0 };
println!("origin at ({}, {}), area = {:.2}", center.x, center.y, s.area());
}Follow-up Questions
- How do enum variants with data differ from separate structs?
- What does 'make illegal states unrepresentable' mean in Rust?
- Why does match have to be exhaustive over an enum?
- When would you use a tuple struct instead of a named-field struct?
MCQ Practice
1. Which statement best describes a Rust enum?
An enum is a sum type: a value is exactly one variant at a given time, unlike a struct which holds all fields at once.
2. What forces you to handle every variant of an enum?
match is checked for exhaustiveness by the compiler, so every enum variant must be covered or the code will not compile.
Flash Cards
Struct in one word — Product type — an AND of fields all present together.
Enum in one word — Sum type — an OR of variants, exactly one at a time.
Can enum variants hold data? — Yes, each variant can carry its own differently-shaped fields.
Why prefer enum over bool flags? — It makes illegal states unrepresentable and forces exhaustive handling.