Function tokio::sync::watch::channel

source ·
pub fn channel<T>(init: T) -> (Sender<T>, Receiver<T>)
Expand description

Creates a new watch channel, returning the “send” and “receive” handles.

All values sent by Sender will become visible to the Receiver handles. Only the last value sent is made available to the Receiver half. All intermediate values are dropped.

§Examples

use tokio::sync::watch;

    let (tx, mut rx) = watch::channel("hello");

    tokio::spawn(async move {
        while rx.changed().await.is_ok() {
            println!("received = {:?}", *rx.borrow());
        }
    });

    tx.send("world")?;