Struct — custom type with fields (single shape). Enum — type with multiple variants.
Struct:
impl blocks.1struct Config {2 host: String,3 port: u16,4}56impl Config {7 fn new(host: &str, port: u16) -> Self {8 Self { host: host.to_string(), port }9 }10}
Enum:
match.1enum Event {2 Click(u32, u32),3 Key(char),4 Resize(u32, u32),5}67fn handle(event: Event) {8 match event {9 Event::Click(x, y) => println!("Click at ({}, {})", x, y),10 Event::Key(c) => println!("Key: {}", c),11 Event::Resize(w, h) => println!("Resize: {}x{}", w, h),12 }13}
When to use:
Common mistakes:
match.