std-method
Mutate an initialized MaybeUninit value
The mutable reference API is for an already valid object, not for constructing one out of uninitialized fields. A normal write initializes the slot first. A mutable reference then gives exclusive access to that object. Expected stdout (not executed): ab Common mistake: Trying to initialize an invalid String through assume_init_mut.
Formal Syntax & Structure
rust syntaxrust
unsafe { slot.assume_init_mut() }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { use std::mem::MaybeUninit; let mut slot = MaybeUninit::new(String::from("a")); // SAFETY: slot contains a valid String and we borrow it exclusively. unsafe { slot.assume_init_mut() }.push('b'); // SAFETY: String remains initialized and no borrow survives. let value = unsafe { slot.assume_init() }; println!("{value}");}