std-method

Transfer a live value with ManuallyDrop::take

Use take only when a consuming extraction does not fit the ownership structure. It moves the value out semantically while leaving the wrapper accessible at the type level. In this narrow local example the wrapper is never used again; prefer into_inner in ordinary code. Expected stdout (not executed): moved Common mistake: Dereferencing, deriving Debug for, or moving an enclosing object after manual extraction without proving its validity.

Formal Syntax & Structure

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

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::ManuallyDrop;    let mut slot = ManuallyDrop::new(String::from("moved"));    // SAFETY: slot owns a live String; this is its final access.    let owned = unsafe { ManuallyDrop::take(&mut slot) };    println!("{owned}");}
Rust Reference