Skip to content

Chapter 19-2: Update text to let...else #4310

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions listings/ch19-patterns-and-matching/listing-19-10/output.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
$ cargo run
Compiling patterns v0.1.0 (file:///projects/patterns)
warning: irrefutable `if let` pattern
--> src/main.rs:2:8
warning: irrefutable `let...else` pattern
--> src/main.rs:2:5
|
2 | if let x = 5 {
| ^^^^^^^^^
2 | let x = 5 else {
| ^^^^^^^^^
|
= note: this pattern will always match, so the `if let` is useless
= help: consider replacing the `if let` with a `let`
= note: this pattern will always match, so the `else` clause is useless
= help: consider removing the `else` clause
= note: `#[warn(irrefutable_let_patterns)]` on by default

warning: `patterns` (bin "patterns") generated 1 warning
Expand Down
19 changes: 9 additions & 10 deletions src/ch19-02-refutability.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ pattern `Some(x)`, Rust rightfully produces a compiler error.

If we have a refutable pattern where an irrefutable pattern is needed, we can
fix it by changing the code that uses the pattern: instead of using `let`, we
can use `if let`. Then if the pattern doesn’t match, the code will just skip
the code in the curly brackets, giving it a way to continue validly. Listing
19-9 shows how to fix the code in Listing 19-8.
can use `let...else`. Then if the pattern doesn’t match, the code in the curly
brackets will be executed, giving it a way to continue validly. Listing 19-9
shows how to fix the code in Listing 19-8.

<Listing number="19-9" caption="Using `let...else` and a block with refutable patterns instead of `let`">

Expand All @@ -62,21 +62,20 @@ the code in the curly brackets, giving it a way to continue validly. Listing

</Listing>

We’ve given the code an out! This code is perfectly valid now. However,
if we give `if let` an irrefutable pattern (a pattern that will always
match), such as `x`, as shown in Listing 19-10, the compiler will give a
warning.
We’ve given the code an out! This code is perfectly valid now. However, if we
give `let...else` an irrefutable pattern (a pattern that will always match),
such as `x`, as shown in Listing 19-10, the compiler will give a warning.

<Listing number="19-10" caption="Attempting to use an irrefutable pattern with `if let`">
<Listing number="19-10" caption="Attempting to use an irrefutable pattern with `let...else`">

```rust
{{#rustdoc_include ../listings/ch19-patterns-and-matching/listing-19-10/src/main.rs:here}}
```

</Listing>

Rust complains that it doesn’t make sense to use `if let` with an irrefutable
pattern:
Rust complains that it doesn’t make sense to use `let...else` with an
irrefutable pattern:

```console
{{#include ../listings/ch19-patterns-and-matching/listing-19-10/output.txt}}
Expand Down