解构

When working with tuples and other structured values it’s common to want to extract the inner values into local variables. This can be done manually by directly accessing the inner values:

  1. fn print_tuple(tuple: (i32, i32)) {
  2. let left = tuple.0;
  3. let right = tuple.1;
  4. println!("left: {left}, right: {right}");
  5. }

However, Rust also supports using pattern matching to destructure a larger value into its constituent parts:

  1. fn print_tuple(tuple: (i32, i32)) {
  2. let (left, right) = tuple;
  3. println!("left: {left}, right: {right}");
  4. }

This slide should take about 5 minutes.

  • The patterns used here are “irrefutable”, meaning that the compiler can statically verify that the value on the right of = has the same structure as the pattern.
  • A variable name is an irrefutable pattern that always matches any value, hence why we can also use let to declare a single variable.
  • Rust also supports using patterns in conditionals, allowing for equality comparison and destructuring to happen at the same time. This form of pattern matching will be discussed in more detail later.
  • Edit the examples above to show the compiler error when the pattern doesn’t match the value being matched on.