std-type

Rust MaybeUninit<T>

MaybeUninit provides storage that is allowed to be not yet a valid T. Keep it wrapped until a valid value has been written. The compiler does not track completion of manual initialization; the programmer must prove it before assume_init. Ordinary Option is usually preferable for optional data. Expected stdout (not executed): true Common mistake: Using assume_init before a valid T has been constructed.

Formal Syntax & Structure

rust syntaxrust
MaybeUninit::<T>::uninit()

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::MaybeUninit;    let mut slot = MaybeUninit::<bool>::uninit();    slot.write(true);    // SAFETY: write initialized the entire bool with a valid value.    let value = unsafe { slot.assume_init() };    println!("{value}");}
Rust Reference