Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Briefly touch on match ergonomics #2581

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/pattern-matching/destructuring-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Like tuples, Struct can also be destructured by matching:

- 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.
- Try `match &foo` and check the type of captures. The pattern syntax remains
the same, but the captures become shared references. This is
[match ergonomics](https://rust-lang.github.io/rfcs/2005-match-ergonomics.html)
and is often useful with `match self` when implementing methods on an enum.
- The same effect occurs with `match &mut foo`: the captures become exclusive
references.
- 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.
Expand Down
Loading