Struct — custom type with named fields (one fixed shape). Enum — type with multiple possible variants.
Struct:
impl blocks.1struct Person {2 name: String,3 age: u32,4}56impl Person {7 fn greet(&self) -> String {8 format!("Hi, I'm {}", self.name)9 }10}
Enum:
match for exhaustive pattern matching.1enum Shape {2 Circle(f64), // radius3 Rectangle(f64, f64), // width, height4 Square(f64), // side5}67fn area(shape: &Shape) -> f64 {8 match shape {9 Shape::Circle(r) => std::f64::consts::PI * r * r,10 Shape::Rectangle(w, h) => w * h,11 Shape::Square(s) => s * s,12 }13}
When to use:
Common mistakes:
match — compiler errors.#[derive()] on enums for common traits.