std-method

Read a Copy value from MaybeUninit

This operation produces a T without consuming the wrapper. Repeated reads of a Copy number are fine, but repeating the operation on an owning String can create multiple owners of the same allocation. Prefer assume_init when ownership should move once. Expected stdout (not executed): 24 Common mistake: Reading a non-Copy owner twice and dropping both results.

Formal Syntax & Structure

rust syntaxrust
unsafe { slot.assume_init_read() }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::MaybeUninit;    let slot = MaybeUninit::new(12_u32);    // SAFETY: initialized u32, Copy, no owned resources are duplicated.    let first = unsafe { slot.assume_init_read() };    // SAFETY: same initialized Copy value remains available.    let second = unsafe { slot.assume_init_read() };    println!("{}", first + second);}
Rust Reference