std-method

Borrow an initialized MaybeUninit value

A shared borrow is valid only after the slot contains a valid value. It does not transfer ownership or arrange destruction. In this example the String is eventually recovered by consuming the slot. Expected stdout (not executed): ready Common mistake: Creating a reference to a value before initialization.

Formal Syntax & Structure

rust syntaxrust
unsafe { slot.assume_init_ref() }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::MaybeUninit;    let mut slot = MaybeUninit::uninit();    slot.write(String::from("ready"));    // SAFETY: the complete String was written; this borrow ends before moving it.    println!("{}", unsafe { slot.assume_init_ref() });    // SAFETY: the initialized value has not been moved or destroyed.    drop(unsafe { slot.assume_init() });}
Rust Reference