Destructuring binds the parts of a composite value to separate variables by matching a pattern. With a tuple, let (a, b, c) = values; pulls its three items apart. section 3.1.3
Keep these rules in mind:
- Make the pattern match the value. A three-item tuple requires three pattern positions; a two-item pattern does not match. section 3.1.3
- Use _ for a component you want to ignore, as in let (_, b, c) = tuple_of_three;. section 3.1.3
- Remember ownership: destructuring a tuple of String values moves those values into the new variables. section 3.1.3
- Apply the same idea to structs, rename bindings with field: new_name, and use .. to ignore remaining fields. section 4.2
- Place a struct pattern in a function parameter when the function should receive selected fields directly. section 4.2
Selecting tuple values
fn main() { let tuple_of_three = ("one", "two", "three"); let (_, b, c) = tuple_of_three; } The underscore leaves the first item unbound, while b and c receive the second and third values. section 3.1.3
Destructuring a struct
let Person { name: fake_name, real_name, height: cm, happiness, } = papa_doc; This binds the name field to fake_name and height to cm while keeping the other field names as bindings. section 4.2





