Enumerations Beyond Simple Tags

Updated

October 31, 2026

Enumerations Beyond Simple Tags

Rust enums are algebraic data types (tagged unions). Each variant can hold different types and amounts of data:

#[derive(Debug)]
enum WebEvent {
    PageLoad,
    KeyPress(char),
    Click { x: i64, y: i64 },
    Submit(String),
}

fn main() {
    let click = WebEvent::Click { x: 120, y: 340 };
    let key = WebEvent::KeyPress('q');
    println!("Events: {:?}, {:?}", click, key);
}