expression

Drop a suspended future: cancellation boundary

Dropping an owned suspended future ends that computation and destroys its stored values. It cannot undo completed external effects, and dropping a handle to an independently spawned task may follow different rules. The guard below records local destruction only. Expected stdout (not executed): true true Common mistake: Assuming dropping a future rolls back I/O or database effects.

Formal Syntax & Structure

rust syntaxrust
drop(owned_future);

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Wake, Waker}, sync::Arc};    struct Noop;    impl Wake for Noop { fn wake(self: Arc<Self>) {} }    let w = Waker::from(Arc::new(Noop));    let mut cx = Context::from_waker(&w);    use std::cell::Cell;    struct Guard<'a>(&'a Cell<bool>);    impl Drop for Guard<'_> { fn drop(&mut self) { self.0.set(true); } }    let dropped = Cell::new(false);    let mut f = Box::pin(async {        let _guard = Guard(&dropped);        std::future::pending::<()>().await;    });    println!("{}", f.as_mut().poll(&mut cx).is_pending());    drop(f);    println!("{}", dropped.get());}
Rust Reference