Skip to content

Commit

Permalink
Merge pull request #2040 from UnHumbleBen/patch-3
Browse files Browse the repository at this point in the history
Fixes #2039
  • Loading branch information
steveklabnik authored Oct 24, 2019
2 parents 67c5a6e + 30fe548 commit 09d26dd
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/ch18-02-refutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ a_value` because if the value in the `a_value` variable is `None` rather than

Function parameters, `let` statements, and `for` loops can only accept
irrefutable patterns, because the program cannot do anything meaningful when
values don’t match. The `if let` and `while let` expressions only accept
refutable patterns, because by definition they’re intended to handle possible
values don’t match. The `if let` and `while let` expressions accept
refutable and irrefutable patterns, but the compiler warns against
irrefutable patterns because by definition they’re intended to handle possible
failure: the functionality of a conditional is in its ability to perform
differently depending on success or failure.

Expand Down Expand Up @@ -69,9 +70,9 @@ patterns instead of `let`</span>
We’ve given the code an out! This code is perfectly valid, although it means we
cannot use an irrefutable pattern without receiving an error. If we give `if
let` a pattern that will always match, such as `x`, as shown in Listing 18-10,
it will not compile.
the compiler will give a warning.

```rust,ignore,does_not_compile
```rust,ignore
if let x = 5 {
println!("{}", x);
};
Expand All @@ -84,11 +85,15 @@ Rust complains that it doesn’t make sense to use `if let` with an irrefutable
pattern:

```text
error[E0162]: irrefutable if-let pattern
--> <anon>:2:8
warning: irrefutable if-let pattern
--> <anon>:2:5
|
2 | / if let x = 5 {
3 | | println!("{}", x);
4 | | };
| |_^
|
2 | if let x = 5 {
| ^ irrefutable pattern
= note: #[warn(irrefutable_let_patterns)] on by default
```

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

0 comments on commit 09d26dd

Please sign in to comment.