async-book/src/05_streams/01_chapter.md

629 B

The Stream Trait

The Stream trait is similar to Future but can yield multiple values before completing, similar to the Iterator trait from the standard library:

{{#include ../../examples/05_01_streams/src/lib.rs:stream_trait}}

One common example of a Stream is the Receiver for the channel type from the futures crate. It will yield Some(val) every time a value is sent from the Sender end, and will yield None once the Sender has been dropped and all pending messages have been received:

{{#include ../../examples/05_01_streams/src/lib.rs:channels}}