expression

Transform and filter a stream

Compose lazy adapters to change or select values as the consumer asks for them. The filter here receives a reference to each item. It does not automatically spawn a task per item. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 20 40 Common mistake: Assuming map executes every transformation before the stream is polled.

Formal Syntax & Structure

rust syntaxrust
StreamExt::map; StreamExt::filter

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio_stream::StreamExt;    let mut s = tokio_stream::iter(1..=5).filter(|n| n % 2 == 0).map(|n| n * 10);    while let Some(n) = s.next().await { println!("{n}"); }}
Rust Reference