@@ -10,8 +10,9 @@ a_value` because if the value in the `a_value` variable is `None` rather than
10
10
11
11
Function parameters, ` let ` statements, and ` for ` loops can only accept
12
12
irrefutable patterns, because the program cannot do anything meaningful when
13
- values don’t match. The ` if let ` and ` while let ` expressions only accept
14
- refutable patterns, because by definition they’re intended to handle possible
13
+ values don’t match. The ` if let ` and ` while let ` expressions accept
14
+ refutable and irrefutable patterns, but the compiler warns against
15
+ irrefutable patterns because by definition they’re intended to handle possible
15
16
failure: the functionality of a conditional is in its ability to perform
16
17
differently depending on success or failure.
17
18
@@ -69,9 +70,9 @@ patterns instead of `let`</span>
69
70
We’ve given the code an out! This code is perfectly valid, although it means we
70
71
cannot use an irrefutable pattern without receiving an error. If we give `if
71
72
let` a pattern that will always match, such as ` x`, as shown in Listing 18-10,
72
- it will not compile .
73
+ the compiler will give a warning .
73
74
74
- ``` rust,ignore,does_not_compile
75
+ ``` rust,ignore
75
76
if let x = 5 {
76
77
println!("{}", x);
77
78
};
@@ -84,11 +85,15 @@ Rust complains that it doesn’t make sense to use `if let` with an irrefutable
84
85
pattern:
85
86
86
87
``` text
87
- error[E0162]: irrefutable if-let pattern
88
- --> <anon>:2:8
88
+ warning: irrefutable if-let pattern
89
+ --> <anon>:2:5
90
+ |
91
+ 2 | / if let x = 5 {
92
+ 3 | | println!("{}", x);
93
+ 4 | | };
94
+ | |_^
89
95
|
90
- 2 | if let x = 5 {
91
- | ^ irrefutable pattern
96
+ = note: #[warn(irrefutable_let_patterns)] on by default
92
97
```
93
98
94
99
For this reason, match arms must use refutable patterns, except for the last
0 commit comments