structs

Similarly, a struct can be destructured as shown:

  1. fn main() {
  2. struct Foo { x: (u32, u32), y: u32 }
  3. // destructure members of the struct
  4. let foo = Foo { x: (1, 2), y: 3 };
  5. let Foo { x: (a, b), y } = foo;
  6. println!("a = {}, b = {}, y = {} ", a, b, y);
  7. // you can destructure structs and rename the variables,
  8. // the order is not important
  9. let Foo { y: i, x: j } = foo;
  10. println!("i = {:?}, j = {:?}", i, j);
  11. // and you can also ignore some variables:
  12. let Foo { y, .. } = foo;
  13. println!("y = {}", y);
  14. // this will give an error: pattern does not mention field `x`
  15. // let Foo { y } = foo;
  16. }

See also:

Structs, The ref pattern