@@ -16,8 +16,13 @@ fn main() {
16
16
match age() {
17
17
0 => println!("I haven't celebrated my first birthday yet"),
18
18
// 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.
21
26
n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),
22
27
n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),
23
28
// Nothing bound. Return the result.
@@ -37,6 +42,13 @@ fn main() {
37
42
match some_number() {
38
43
// Got `Some` variant, match if its value, bound to `n`,
39
44
// 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)
40
52
Some(n @ 42) => println!("The Answer: {}!", n),
41
53
// Match any other number.
42
54
Some(n) => println!("Not interesting... {}", n),
0 commit comments