expression
CancellationToken cooperative notification
A cancellation token broadcasts a request; it does not forcibly kill tasks. A task may observe a request even when it starts waiting after cancel was called. Join the worker to confirm exit. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): stopped Common mistake: Treating cancel() as proof that all cleanup finished.
Formal Syntax & Structure
rust syntaxrust
CancellationToken::new(); token.cancel(); token.cancelled().awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use tokio_util::sync::CancellationToken; let token = CancellationToken::new(); let child = token.clone(); let task = tokio::spawn(async move { child.cancelled().await; "stopped" }); token.cancel(); println!("{}", task.await.unwrap());}