Skip to content

Commit 09d26dd

Browse files
authored
Merge pull request #2040 from UnHumbleBen/patch-3
Fixes #2039
2 parents 67c5a6e + 30fe548 commit 09d26dd

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/ch18-02-refutability.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ a_value` because if the value in the `a_value` variable is `None` rather than
1010

1111
Function parameters, `let` statements, and `for` loops can only accept
1212
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
1516
failure: the functionality of a conditional is in its ability to perform
1617
differently depending on success or failure.
1718

@@ -69,9 +70,9 @@ patterns instead of `let`</span>
6970
We’ve given the code an out! This code is perfectly valid, although it means we
7071
cannot use an irrefutable pattern without receiving an error. If we give `if
7172
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.
7374

74-
```rust,ignore,does_not_compile
75+
```rust,ignore
7576
if 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
8485
pattern:
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

9499
For this reason, match arms must use refutable patterns, except for the last

0 commit comments

Comments
 (0)