MaybeUninit and ManuallyDrop
Quick answer
MaybeUninit<T> can store memory before a valid T exists. ManuallyDrop<T> contains an already valid T but suppresses its automatic destructor. They solve different problems.
Track ownership and initialization
Do not call assume_init until every required part of T is initialized and valid. Dropping MaybeUninit does not drop an initialized inner value, so partial construction needs a cleanup plan on errors or panic. ManuallyDrop is not uninitialized storage; use into_inner to regain normal ownership when possible. Avoid exposing manually destroyed values through safe methods or derived traits.
use std::mem::MaybeUninit;fn main() { let mut slot = MaybeUninit::<String>::uninit(); slot.write(String::from("ready")); // SAFETY: write initialized the complete String; ownership transfers once. let s = unsafe { slot.assume_init() }; println!("{s}");}Expected stdout (not executed):
readyThe String now has normal ownership and is dropped normally.
use std::mem::ManuallyDrop;fn main() { let held = ManuallyDrop::new(String::from("owned")); let s = ManuallyDrop::into_inner(held); println!("{s}");}Expected stdout (not executed):
ownedThe wrapper is consumed; the returned String owns its allocation.
Common mistake
Do not assume zeroed bytes form a valid value of every type or manually drop the same resource twice.
Try it yourself
Explain why an array initialized one element at a time needs cleanup of its initialized prefix if construction fails.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
MaybeUninit and ManuallyDrop — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An array of MaybeUninit<String> is partially filled before an error. What is needed?
MaybeUninit and ManuallyDrop — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::mem::MaybeUninit; fn main() { let mut slot = MaybeUninit::<String>::uninit(); slot.write(String::from("ready")); // SAFETY: write initialized the complete String; ownership transfers once. let s = unsafe { slot.assume_init() }; println!("{s}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
MaybeUninit and ManuallyDrop — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::mem::ManuallyDrop; fn main() { let held = ManuallyDrop::new(String::from("owned")); let s = ManuallyDrop::into_inner(held); println!("{s}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.