@@ -98,23 +98,25 @@ not alert us to the possible logic bug.
98
98
### ` while let ` Conditional Loops
99
99
100
100
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 ` .
104
106
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 ` " >
106
108
107
109
``` rust
108
110
{{#rustdoc_include .. / listings / ch19 - patterns - and - matching / listing - 19 - 02 / src / main . rs: here }}
109
111
```
110
112
111
113
</Listing >
112
114
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 .
118
120
119
121
### ` for ` Loops
120
122
0 commit comments