expression

Subscribe to the current watch state

A new receiver starts with the latest retained value considered observed. Read that initial snapshot explicitly if your task needs it; subsequent changed calls wait for new versions. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 8 false 9 Common mistake: Waiting on changed before reading the initial snapshot a worker needs.

Formal Syntax & Structure

rust syntaxrust
tx.subscribe() -> watch::Receiver<T>

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let (tx, _rx) = tokio::sync::watch::channel(1);    tx.send(8).unwrap();    let mut late = tx.subscribe();    println!("{} {}", *late.borrow(), late.has_changed().unwrap());    tx.send(9).unwrap();    late.changed().await.unwrap();    println!("{}", *late.borrow());}
Rust Reference