Skip to content

Commit eca0a61

Browse files
committed
update original
1 parent b51c08a commit eca0a61

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
fn main() {
22
// ANCHOR: here
3-
let mut stack = Vec::new();
3+
let (tx, rx) = std::sync::mpsc::channel();
4+
std::thread::spawn(move || {
5+
for val in [1, 2, 3] {
6+
tx.send(val).unwrap();
7+
}
8+
});
49

5-
stack.push(1);
6-
stack.push(2);
7-
stack.push(3);
8-
9-
while let Some(top) = stack.pop() {
10-
println!("{top}");
10+
while let Ok(value) = rx.recv() {
11+
println!("{value}");
1112
}
1213
// ANCHOR_END: here
1314
}

rustbook-en/src/ch17-02-concurrency-with-async.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,6 @@ loop is the loop version of the `if let` construct we saw back in Chapter 6. The
242242
loop will continue executing as long as the pattern it specifies continues to
243243
match the value.
244244

245-
<!-- TODO: update text in ch. 19 to account for our having introduced this. -->
246-
247245
The `rx.recv` call produces a `Future`, which we await. The runtime will pause
248246
the `Future` until it is ready. Once a message arrives, the future will resolve
249247
to `Some(message)`, as many times as a message arrives. When the channel closes,

rustbook-en/src/ch19-01-all-the-places-for-patterns.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,25 @@ not alert us to the possible logic bug.
9898
### `while let` Conditional Loops
9999

100100
Similar in construction to `if let`, the `while let` conditional loop allows a
101-
`while` loop to run for as long as a pattern continues to match. In Listing
102-
19-2 we code a `while let` loop that uses a vector as a stack and prints the
103-
values in the vector in the opposite order in which they were pushed.
101+
`while` loop to run for as long as a pattern continues to match. We first saw a
102+
`while let` loop in Chapter 17, where we used it to keep looping as long as a
103+
stream produced new values. Similarly, in Listing 19-2 we show a `while let`
104+
loop that waits on messages sent between threads, but in this case checking a
105+
`Result` instead of an `Option`.
104106

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

107109
```rust
108110
{{#rustdoc_include ../listings/ch19-patterns-and-matching/listing-19-02/src/main.rs:here}}
109111
```
110112

111113
</Listing>
112114

113-
This example prints 3, 2, and then 1. The `pop` method takes the last element
114-
out of the vector and returns `Some(value)`. If the vector is empty, `pop`
115-
returns `None`. The `while` loop continues running the code in its block as
116-
long as `pop` returns `Some`. When `pop` returns `None`, the loop stops. We can
117-
use `while let` to pop every element off our stack.
115+
This example prints 1, 2, and 3. When we saw `recv` back in Chapter 16, we
116+
unwrapped the error directly, or interacted with it as an iterator using a `for`
117+
loop. As Listing 19-2 shows, though, we can also use `while let`, because the
118+
`recv` method returns `Ok` as long as the sender is producing messages, and then
119+
produces an `Err` once the sender side disconnects.
118120

119121
### `for` Loops
120122

0 commit comments

Comments
 (0)