ManuallyDrop

ManuallyDrop<T> wraps a value so its destructor is not run automatically; you call ManuallyDrop::drop (unsafe) yourself, or leak it deliberately. It gives precise control of drop order and ownership in unsafe code, at zero runtime cost.

What it is

A transparent wrapper that suppresses automatic Drop. Reading the inner value is safe; running its destructor exactly once is your responsibility.

Example

use std::mem::ManuallyDrop;
 
struct Noisy;
impl Drop for Noisy {
    fn drop(&mut self) { println!("dropped"); }
}
 
fn main() {
    let mut m = ManuallyDrop::new(Noisy);
    // ... use &*m ...
    // SAFETY: dropped exactly once and not used afterwards.
    unsafe { ManuallyDrop::drop(&mut m); }
}

Best practice

  • ✅ Use it only in unsafe/FFI code needing manual drop ordering; document the SAFETY invariant.

Pitfalls

See also

The Drop Trait · MaybeUninit · Pin projection · Undefined Behavior · unsafe fn · Raw Pointers · Unsafe Rust & FFI

Sources