Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2039 #2040

Merged
merged 2 commits into from
Oct 24, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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