O'Reilly logo

Network Programming with Rust by Abhishek Chanda

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required