expression
Read and mark a watch value as observed
Reading with borrow leaves observation tracking unchanged; borrow_and_update records the observed version. Copy or clone the needed state in a short scope before awaiting other operations; the borrowed guard holds a read lock. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 20 true 20 false Common mistake: Holding the borrowed watch reference across an await and blocking an update.
Formal Syntax & Structure
rust syntaxrust
rx.borrow(); rx.borrow_and_update()Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { let (tx, mut rx) = tokio::sync::watch::channel(10); tx.send(20).unwrap(); println!("{}", *rx.borrow()); println!("{}", rx.has_changed().unwrap()); let value = *rx.borrow_and_update(); println!("{value} {}", rx.has_changed().unwrap());}