Skip to content

Commit

Permalink
Ch. 19: discuss while let referencing ch. 17
Browse files Browse the repository at this point in the history
This is no longer the first time people see `while let` in the book, so
(a) update the text to mention Chapter 17, and (b) change the example
slightly so it shows something other than the `Option`-based scenario
readers will have already seen in much more interesting ways back in the
earlier part of the book.
  • Loading branch information
chriskrycho committed Oct 16, 2024
1 parent 1cbfc5c commit 3bcb09f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/ch17-02-concurrency-with-async.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ loop is the loop version of the `if let` construct we saw back in Chapter 6. The
loop will continue executing as long as the pattern it specifies continues to
match the value.

<!-- TODO: update text in ch. 19 to account for our having introduced this. -->

The `rx.recv` call produces a `Future`, which we await. The runtime will pause
the `Future` until it is ready. Once a message arrives, the future will resolve
to `Some(message)`, as many times as a message arrives. When the channel closes,
Expand Down
18 changes: 12 additions & 6 deletions src/ch19-01-all-the-places-for-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ not alert us to the possible logic bug.
### `while let` Conditional Loops

Similar in construction to `if let`, the `while let` conditional loop allows a
`while` loop to run for as long as a pattern continues to match. In Listing
19-2 we code a `while let` loop that uses a vector as a stack and prints the
values in the vector in the opposite order in which they were pushed.
`while` loop to run for as long as a pattern continues to match. We first saw a
`while let` loop in Chapter 17, where we used it to keep looping as long as a
stream produced new values. Similarly, in Listing 19-2 we code a `while let`
loop that uses a vector as a stack and prints the values in the vector in the
opposite order in which they were pushed. We also use a new helper function,
`ok_or`, which converts an `Option` into a `Result`, to show how `while let` can
work with any valid pattern.

<Listing number="19-2" caption="Using a `while let` loop to print values for as long as `stack.pop()` returns `Some`">

Expand All @@ -112,9 +116,11 @@ values in the vector in the opposite order in which they were pushed.

This example prints 3, 2, and then 1. The `pop` method takes the last element
out of the vector and returns `Some(value)`. If the vector is empty, `pop`
returns `None`. The `while` loop continues running the code in its block as
long as `pop` returns `Some`. When `pop` returns `None`, the loop stops. We can
use `while let` to pop every element off our stack.
returns `None`. The `ok_or` function turns `Some(value)` into an `Ok(value)` and
turns the `None` for the empty vector into an `Err("empty!")`. The `while` loop
continues running the code in its block as long as `ok_or` returns `Ok`. When
`ok_or` returns `Err("empty!")`, the loop stops. We can use `while let` to pop
every element off our stack.

### `for` Loops

Expand Down

0 comments on commit 3bcb09f

Please sign in to comment.