expression

Coordinate cancellation and task completion

Stop intake, request shutdown, and wait for tracked tasks. This finite fixture has no live work intake and uses a join handle to verify a worker reached its cleanup branch. In production, define a bounded shutdown deadline and a policy for unfinished work. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 1 Common mistake: Exiting immediately after signalling cancellation and losing pending cleanup.

Formal Syntax & Structure

rust syntaxrust
CancellationToken; TaskTracker::close; wait

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tokio_util::{sync::CancellationToken, task::TaskTracker};    let tracker = TaskTracker::new();    let token = CancellationToken::new();    let worker_token = token.clone();    let worker = tracker.spawn(async move {        worker_token.cancelled().await;        1u32    });    token.cancel();    tracker.close();    tracker.wait().await;    println!("{}", worker.await.unwrap());}
Rust Reference