Skip to content

Commit 6648bf7

Browse files
authored
Merge pull request #1946 from ZylosLumen/master
Document drawbacks of alternatives to match binding
2 parents e386be5 + cef2421 commit 6648bf7

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/flow_control/match/binding.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ fn main() {
1616
match age() {
1717
0 => println!("I haven't celebrated my first birthday yet"),
1818
// Could `match` 1 ..= 12 directly but then what age
19-
// would the child be? Instead, bind to `n` for the
20-
// sequence of 1 ..= 12. Now the age can be reported.
19+
// would the child be?
20+
// Could `match` n and use an `if` guard, but would
21+
// not contribute to exhaustiveness checks.
22+
// (Although in this case that would not matter since
23+
// a "catch-all" pattern is present at the bottom)
24+
// Instead, bind to `n` for the sequence of 1 ..= 12.
25+
// Now the age can be reported.
2126
n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
2227
n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
2328
// Nothing bound. Return the result.
@@ -37,6 +42,13 @@ fn main() {
3742
match some_number() {
3843
// Got `Some` variant, match if its value, bound to `n`,
3944
// is equal to 42.
45+
// Could also use `Some(42)` and print `"The Awnser: 42!"`
46+
// but that would require changing `42` in 2 spots should
47+
// you ever wish to change it.
48+
// Could also use `Some(n) if n == 42` and print `"The Awnser: {n}!"`
49+
// but that would not contribute to exhaustiveness checks.
50+
// (Although in this case that would not matter since
51+
// the next arm is a "catch-all" pattern)
4052
Some(n @ 42) => println!("The Answer: {}!", n),
4153
// Match any other number.
4254
Some(n) => println!("Not interesting... {}", n),

0 commit comments

Comments
 (0)