expression
Consume an asynchronous sequence
A stream can supply multiple values over time. Its asynchronous next operation can wait; this differs from an ordinary synchronous iterator. A stream created from a vector provides an immediate, deterministic example. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 15 Common mistake: Trying to use an ordinary for loop on a general asynchronous stream.
Formal Syntax & Structure
rust syntaxrust
tokio_stream::iter(values); StreamExt::next().awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use tokio_stream::StreamExt; let mut s = tokio_stream::iter([3, 5, 7]); let mut sum = 0; while let Some(n) = s.next().await { sum += n; } println!("{sum}");}