Expand description
Composable asynchronous iteration.
§Examples
Merge multiple streams to handle values as soon as they’re ready, without ever dropping a single value:
use futures_concurrency::prelude::*;
use futures_lite::stream::{self, StreamExt};
use futures_lite::future::block_on;
block_on(async {
let a = stream::once(1);
let b = stream::once(2);
let c = stream::once(3);
let s = (a, b, c).merge();
let mut counter = 0;
s.for_each(|n| counter += n).await;
assert_eq!(counter, 6);
})
§Concurrency
When working with multiple (async) iterators, the ordering in which iterators are awaited is important. As part of async iterators, Rust provides built-in operations to control the order of execution of sets of iterators:
merge
: combine multiple iterators into a single iterator, where the new iterator yields an item as soon as one is available from one of the underlying iterators.zip
: combine multiple iterators into an iterator of pairs. The underlying iterators will be awaited concurrently.chain
: iterate over multiple iterators in sequence. The next iterator in the sequence won’t start until the previous iterator has finished.
§Futures
Futures can be thought of as async sequences of single items. Using
stream::once
, futures can be converted into async iterators and then used
with any of the iterator concurrency methods. This enables operations such
as stream::Merge
to be used to execute sets of futures concurrently, but
obtain the individual future’s outputs as soon as they’re available.
See the future concurrency documentation for more on futures concurrency.
Modules§
- A growable group of streams which act as a single unit.
Structs§
- A growable group of streams which act as a single unit.
- Delay execution of a stream once for the specified duration.
Traits§
- Takes multiple streams and creates a new stream over all in sequence.
- Conversion into a
Stream
. - Combines multiple streams into a single stream of all their outputs.
- An extension trait for the
Stream
trait. - ‘Zips up’ multiple streams into a single stream of pairs.