Skip to content

Commit

Permalink
Add speaker note about @ in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Sep 20, 2024
1 parent 2b58efa commit 2b1a590
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/pattern-matching/match.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,24 @@ Key Points:
- The condition defined in the guard applies to every expression in a pattern
with an `|`.

# More To Explore

- Another piece of pattern syntax you can show students is the `@` syntax which
binds a part of a pattern to a variable. For example:

```rust
let opt = Some(123);
match opt {
outer @ Some(inner) => {
println!("outer: {outer:?}, inner: {inner}");
}
None => {}
}
```

In this example `inner` has the value 123 which it pulled from the `Option`
via destructuring, `outer` captures the entire `Some(inner)` expression, so it
contains the full `Option::Some(123)`. This is rarely used but can be useful
in more complex patterns.

</details>

0 comments on commit 2b1a590

Please sign in to comment.