Skip to content

Document drawbacks of alternatives to match binding #1946

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

Merged
merged 1 commit into from
Aug 2, 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
16 changes: 14 additions & 2 deletions src/flow_control/match/binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ fn main() {
match age() {
0 => println!("I haven't celebrated my first birthday yet"),
// Could `match` 1 ..= 12 directly but then what age
// would the child be? Instead, bind to `n` for the
// sequence of 1 ..= 12. Now the age can be reported.
// would the child be?
// Could `match` n and use an `if` guard, but would
// not contribute to exhaustiveness checks.
// (Although in this case that would not matter since
// a "catch-all" pattern is present at the bottom)
// Instead, bind to `n` for the sequence of 1 ..= 12.
// Now the age can be reported.
n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
// Nothing bound. Return the result.
Expand All @@ -37,6 +42,13 @@ fn main() {
match some_number() {
// Got `Some` variant, match if its value, bound to `n`,
// is equal to 42.
// Could also use `Some(42)` and print `"The Awnser: 42!"`
// but that would require changing `42` in 2 spots should
// you ever wish to change it.
// Could also use `Some(n) if n == 42` and print `"The Awnser: {n}!"`
// but that would not contribute to exhaustiveness checks.
// (Although in this case that would not matter since
// the next arm is a "catch-all" pattern)
Some(n @ 42) => println!("The Answer: {}!", n),
// Match any other number.
Some(n) => println!("Not interesting... {}", n),
Expand Down