expression

Reserve a message slot before producing data

Acquire a slot before constructing a value. The permit temporarily holds capacity; sending consumes it, and dropping it unused releases the slot. This does not reserve a fixed number of bytes. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 0 report Common mistake: Reserving capacity and forgetting that an unused permit can keep the queue full.

Formal Syntax & Structure

rust syntaxrust
tx.reserve().await -> Result<Permit, SendError<()>>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, mut rx) = tokio::sync::mpsc::channel(1);    let slot = tx.reserve().await.unwrap();    println!("{}", tx.capacity());    slot.send(String::from("report"));    println!("{}", rx.recv().await.unwrap());}
Rust Reference