@@ -1899,21 +1899,20 @@ help: there is a method `try_next` with a similar name
1899
1899
For more information about this error, try `rustc --explain E0599`.
1900
1900
```
1901
1901
1902
- As the output suggests, the problem is that we need the right trait in scope to
1903
- be able to use the ` next ` method. Given our discussion so far, you might
1904
- reasonably expect that to be ` Stream ` , but the trait we need * here* is actually
1905
- ` StreamExt ` . The ` Ext ` there is for “extension”: this is a common pattern in
1906
- the Rust community for extending one trait with another.
1907
-
1908
- You might be wondering why ` StreamExt ` instead of ` Stream ` , and for that matter
1909
- whether there is a ` Stream ` trait at all. Briefly, the answer is that throughout
1910
- the Rust ecosystem, the ` Stream ` trait defines a low-level interface which
1911
- effectively combines the ` Iterator ` and ` Future ` traits. The ` StreamExt ` trait
1912
- supplies a higher-level set of APIs on top of ` Stream ` , including the ` next `
1913
- method as well as other utility methods similar to those provided by the
1914
- ` Iterator ` trait. We’ll return to the ` Stream ` and ` StreamExt ` traits in a
1915
- bit more detail at the end of the chapter. For now, this is enough to let us
1916
- keep moving.
1902
+ As the output suggests, the reason for the compiler error is that we need the
1903
+ right trait in scope to be able to use the ` next ` method. Given our discussion
1904
+ so far, you might reasonably expect that to be ` Stream ` , but the trait we need
1905
+ here is actually ` StreamExt ` . The ` Ext ` there is for “extension”: this is a
1906
+ common pattern in the Rust community for extending one trait with another.
1907
+
1908
+ Why do we need ` StreamExt ` instead of ` Stream ` , and what does the ` Stream ` trait
1909
+ itself do? Briefly, the answer is that throughout the Rust ecosystem, the
1910
+ ` Stream ` trait defines a low-level interface which effectively combines the
1911
+ ` Iterator ` and ` Future ` traits. The ` StreamExt ` trait supplies a higher-level
1912
+ set of APIs on top of ` Stream ` , including the ` next ` method as well as other
1913
+ utility methods similar to those provided by the ` Iterator ` trait. We’ll return
1914
+ to the ` Stream ` and ` StreamExt ` traits in a bit more detail at the end of the
1915
+ chapter. For now, this is enough to let us keep moving.
1917
1916
1918
1917
The fix to the compiler error is to add a ` use ` statement for ` trpl::StreamExt ` ,
1919
1918
as in Listing 17-31.
@@ -2109,13 +2108,13 @@ Listing 17-35: Sending messages through `tx` with an async delay without making
2109
2108
To sleep between messages in the ` get_messages ` function without blocking, we
2110
2109
need to use async. However, we can’t make ` get_messages ` itself into an async
2111
2110
function, because then we’d return a ` Future<Output = Stream<Item = String>> `
2112
- instead of just a ` Stream<Item = String>> ` . The caller would have to await
2113
- ` get_messages ` itself to get access to the stream. But remember: everything in
2114
- a given future happens linearly; concurrency happens * between* futures.
2115
- Awaiting ` get_messages ` would require it to send all the messages, including
2116
- sleeping between sending each message, before returning the receiver stream. As
2117
- a result, the timeout would end up useless. There would be no delays in the
2118
- stream itself: the delays would all happen before the stream was even available.
2111
+ instead of a ` Stream<Item = String>> ` . The caller would have to await
2112
+ ` get_messages ` itself to get access to the stream. But remember: everything in a
2113
+ given future happens linearly; concurrency happens * between* futures. Awaiting
2114
+ ` get_messages ` would require it to send all the messages, including sleeping
2115
+ between sending each message, before returning the receiver stream. As a result,
2116
+ the timeout would end up useless. There would be no delays in the stream itself:
2117
+ the delays would all happen before the stream was even available.
2119
2118
2120
2119
Instead, we leave ` get_messages ` as a regular function which returns a stream,
2121
2120
and spawn a task to handle the async ` sleep ` calls.
@@ -2557,9 +2556,10 @@ pub trait Future {
2557
2556
}
2558
2557
```
2559
2558
2560
- The ` cx ` parameter and its ` Context ` type is interesting, but is beyond the
2561
- scope of this chapter: you generally only need to worry about it when writing a
2562
- custom ` Future ` implementation.
2559
+ The ` cx ` parameter and its ` Context ` type is the key to how a runtime actually
2560
+ knows when to check any given future, while still being lazy. The details of how
2561
+ that works are beyond the scope of this chapter, though: you generally only need
2562
+ to worry about it when writing a custom ` Future ` implementation.
2563
2563
2564
2564
Instead, we’ll focus on the type for ` self ` . This is the first time we’ve seen
2565
2565
a method where ` self ` has a type annotation. A type annotation for ` self ` is
0 commit comments