Structs

Like tuples, Struct can also be destructured by matching:

1. ```
   struct Foo {
   ```
2. ```
       x: (u32, u32),
   ```
3. ```
       y: u32,
   ```
4. ```
   }
   ```

6. ```
   #[rustfmt::skip]
   ```
7. ```
   fn main() {
   ```
8. ```
       let foo = Foo { x: (1, 2), y: 3 };
   ```
9. ```
       match foo {
   ```
10. ```
            Foo { x: (1, b), y } => println!("x.0 = 1, b = {b}, y = {y}"),
    ```
11. ```
            Foo { y: 2, x: i }   => println!("y = 2, x = {i:?}"),
    ```
12. ```
            Foo { y, .. }        => println!("y = {y}, other fields were ignored"),
    ```
13. ```
        }
    ```
14. ```
    }
    ```

This slide should take about 4 minutes.

  • Change the literal values in foo to match with the other patterns.
  • Add a new field to Foo and make changes to the pattern as needed.
  • The distinction between a capture and a constant expression can be hard to spot. Try changing the 2 in the second arm to a variable, and see that it subtly doesn’t work. Change it to a const and see it working again.