expression
CancellationToken parent and child scopes
A child inherits cancellation from its parent, but cancelling the child does not cancel the parent. Clones instead share the same cancellation state. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): false true true Common mistake: Using clone when independent child cancellation is required.
Formal Syntax & Structure
rust syntaxrust
token.child_token(); child.cancel()Working Example
Example Snippetrust
fn main() { use tokio_util::sync::CancellationToken; let parent = CancellationToken::new(); let child = parent.child_token(); let sibling = parent.child_token(); child.cancel(); println!("{} {}", parent.is_cancelled(), child.is_cancelled()); parent.cancel(); println!("{}", sibling.is_cancelled());}