How to Destructure Values in Rust

Use match patterns with curly braces or parentheses to extract and name specific values from Rust enums and structs.

You destructure values in Rust by using patterns inside a match expression to bind parts of a value to new variables. For structs and enums with named fields, use curly braces { x, y } to extract them; for tuple-like variants, use parentheses (r, g, b).

enum Message {
    Quit,
    Move { x: i32, y: i32 },
    Write(String),
    ChangeColor(i32, i32, i32),
}

fn main() {
    let msg = Message::ChangeColor(0, 160, 255);

    match msg {
        Message::Quit => println!("Quit"),
        Message::Move { x, y } => println!("Move {x}, {y}"),
        Message::Write(text) => println!("Text: {text}"),
        Message::ChangeColor(r, g, b) => println!("Color: {r}, {g}, {b}"),
    }
}