Working with streams and sinks

The futures crate provides another useful abstraction for a lazily evaluated series of events, called Stream. If Future corresponds to Result, a Stream corresponds to Iterator. Semantically, they are very similar to futures, and they look like this:

trait Stream {    type Item;    type Error;    fn poll(& mut self) -> Poll<Option<Self::Item>, Self::Error>;    ...}

The only difference here is that the return type is wrapped in an Option, exactly like the Iterator trait. Thus, a None here would indicate that the stream has terminated. Also, all streams are futures and can be converted using into_future. Let us look at an example of using this construct. We will partially reuse our collatz example from a previous chapter. ...

Get Network Programming with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.