@@ -10,8 +10,9 @@ a_value` because if the value in the `a_value` variable is `None` rather than
1010
1111Function parameters, ` let ` statements, and ` for ` loops can only accept
1212irrefutable 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
1516failure: the functionality of a conditional is in its ability to perform
1617differently depending on success or failure.
1718
@@ -69,9 +70,9 @@ patterns instead of `let`</span>
6970We’ve given the code an out! This code is perfectly valid, although it means we
7071cannot use an irrefutable pattern without receiving an error. If we give `if
7172let` 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 .
7374
74- ``` rust,ignore,does_not_compile
75+ ``` rust,ignore
7576if let x = 5 {
7677 println!("{}", x);
7778};
@@ -84,11 +85,15 @@ Rust complains that it doesn’t make sense to use `if let` with an irrefutable
8485pattern:
8586
8687``` 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+ | |_^
8995 |
90- 2 | if let x = 5 {
91- | ^ irrefutable pattern
96+ = note: #[warn(irrefutable_let_patterns)] on by default
9297```
9398
9499For this reason, match arms must use refutable patterns, except for the last
0 commit comments