expression

Broadcast messages to multiple subscribers

Use a bounded shared history to distribute events to multiple receivers. Each subscriber has its own cursor. Unlike a worker queue, receiving on one subscriber does not consume another subscriber’s copy. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 2 7 7 Common mistake: Using broadcast as though it assigns each job to exactly one worker.

Formal Syntax & Structure

rust syntaxrust
tokio::sync::broadcast::channel(capacity); tx.subscribe()

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, mut a) = tokio::sync::broadcast::channel(4);    let mut b = tx.subscribe();    println!("{}", tx.send(7).unwrap());    println!("{} {}", a.recv().await.unwrap(), b.recv().await.unwrap());}
Rust Reference