expression

Cancellation on scope exit

A cancellation guard requests cancellation when dropped unless disarmed. Use it for scope cleanup of a cancellation signal, not as proof that asynchronous cleanup ran. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): false true Common mistake: Assuming a Drop guard can await every worker’s cleanup.

Formal Syntax & Structure

rust syntaxrust
token.clone().drop_guard()

Working Example

Example Snippetrust
fn main() {    use tokio_util::sync::CancellationToken;    let token = CancellationToken::new();    {        let _guard = token.clone().drop_guard();        println!("{}", token.is_cancelled());    }    println!("{}", token.is_cancelled());}
Rust Reference