expression
Scoped callback registration model
An asynchronous native callback must stop accessing state before that state is destroyed. This safe, single-threaded model demonstrates a registration borrowing its callback slot and clearing it on Drop. Actual native deregistration must additionally wait for callbacks already in flight; dropping a flag is insufficient. Expected stdout (not executed): event 3 unregistered true Common mistake: Freeing the callback context immediately after an unregister request without a completion guarantee.
Formal Syntax & Structure
rust syntaxrust
Drop for Registration<'_>Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { struct Registration<'a> { slot: &'a mut Option<fn(u32)> } impl Registration<'_> { fn fire(&self, n: u32) { if let Some(f) = *self.slot { f(n); } } } impl Drop for Registration<'_> { fn drop(&mut self) { *self.slot = None; } } fn report(n: u32) { println!("event {n}"); } let mut slot = Some(report as fn(u32)); { let registration = Registration { slot: &mut slot }; registration.fire(3); } println!("unregistered {}", slot.is_none());}