std-type

Rust ManuallyDrop: control automatic destruction

ManuallyDrop suppresses the wrapped value’s automatic destruction. It is an advanced abstraction; prefer ordinary ownership and Drop for typical applications. This example uses only safe construction and extraction, restoring normal ownership without manually invoking a destructor. Expected output (not executed): Wrapped Released Done Why: into_inner returns the wrapped value so normal destruction can occur. Common mistake: Calling unsafe manual destruction and then continuing to access the destroyed value. Advanced layout and movement constraints need version-specific review.

Formal Syntax & Structure

rust syntaxrust
ManuallyDrop::new(value); ManuallyDrop::into_inner(wrapper);

Working Example

Example Snippetrust
use std::mem::ManuallyDrop;struct Log;impl Drop for Log {    fn drop(&mut self) { println!("Released"); }}fn main() {    let wrapped = ManuallyDrop::new(Log);    println!("Wrapped");    let value = ManuallyDrop::into_inner(wrapped);    drop(value);    println!("Done");}
Rust Reference