Destructure a Rust struct by matching its fields with curly braces in a pattern to extract values into variables.
You destructure a struct by using curly braces with field names in a pattern, such as inside a match arm or an if let statement.
struct Point { x: i32, y: i32 }
let p = Point { x: 10, y: 20 };
match p {
Point { x, y } => println!("x: {x}, y: {y}"),
}
This creates variables x and y bound to the struct's field values.
Destructuring a struct is like taking apart a labeled box to get the items inside. Instead of reaching in to grab each item by its label one by one, you open the box and the items automatically appear in your hands with their names. You use this when you need to work with specific parts of a data group without writing repetitive code to access them individually.