std-method

Run a destructor through ManuallyDrop

Explicit destruction requires a live object and must be the final access to that object. The wrapper does not run its destructor automatically. Avoid this pattern when ordinary scope-based destruction suffices; in particular older Rust baselines impose additional restrictions on moving manually destroyed Box-containing wrappers. Expected stdout (not executed): drops=1 Common mistake: Reading or manually dropping a destroyed value again.

Formal Syntax & Structure

rust syntaxrust
unsafe { ManuallyDrop::drop(&mut wrapper) }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::{cell::Cell, mem::ManuallyDrop};    struct Count<'a>(&'a Cell<u32>);    impl Drop for Count<'_> {        fn drop(&mut self) { self.0.set(self.0.get() + 1); }    }    let drops = Cell::new(0);    let mut slot = ManuallyDrop::new(Count(&drops));    // SAFETY: valid live Count, exclusive access, never used or moved afterward.    unsafe { ManuallyDrop::drop(&mut slot); }    println!("drops={}", drops.get());}
Rust Reference