🦀 Rust
Beginner
What is pattern matching with destructuring in Rust?
Answer
Destructuring is Rust's ability to break apart composite types (structs, enums, tuples) into their component parts in a single let binding or match arm. Tuple destructuring: let (x, y) = (1, 2);. Struct destructuring: let Point { x, y } = point;. Enum destructuring in match: match shape { Shape::Circle(radius) => ..., Shape::Rectangle(w, h) => ... }. You can use _ to ignore fields, .. to ignore remaining fields in a struct, and nested destructuring for complex types. Destructuring also works in function parameters: fn print_coords(&(x, y): &(i32, i32)). It is one of Rust's most ergonomic features for working with structured data.
Previous
What are if let and while let in Rust?
Next
What is a panic in Rust and when does it occur?