Struct — custom type with fields (single shape). Enum — type with multiple variants.
Struct:
impl blocks.1// Named fields2struct Config {3 host: String,4 port: u16,5 debug: bool,6}78// Tuple struct9struct Meters(f64);1011// Unit struct12struct Marker;1314impl Config {15 fn new(host: String, port: u16) -> Self {16 Self { host, port, debug: false }17 }18}
Enum:
match ensures all cases are handled.1enum Command {2 Quit,3 Echo(String),4 Move { x: i32, y: i32 },5}67fn execute(cmd: Command) {8 match cmd {9 Command::Quit => println!("Quitting"),10 Command::Echo(msg) => println!("{}", msg),11 Command::Move { x, y } => println!("Move to ({}, {})", x, y),12 }13}
When to use:
Common mistakes:
match — compiler enforces exhaustiveness.Debug, Clone, PartialEq.