expression

Wrap a bounded receiver as a stream

Adapt a Tokio bounded message receiver to the stream interface. Queue capacity and producer closure still matter. The adapter does not convert an item-count capacity into a byte budget. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): [4, 6] Common mistake: Keeping a sender alive while waiting for the stream to end.

Formal Syntax & Structure

rust syntaxrust
tokio_stream::wrappers::ReceiverStream::new(rx)

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio_stream::{StreamExt, wrappers::ReceiverStream};    let (tx, rx) = tokio::sync::mpsc::channel(2);    tx.send(4).await.unwrap();    tx.send(6).await.unwrap();    drop(tx);    let values: Vec<_> = ReceiverStream::new(rx).collect().await;    println!("{values:?}");}
Rust Reference