std-method

Destroy one initialized MaybeUninit slot

A slot containing a live resource needs exactly one ownership-ending operation if you intend to release that resource. This method destroys it without returning a value. The wrapper can subsequently hold a new value, but its old contents must not be accessed. Expected stdout (not executed): second Common mistake: Dropping a slot twice without writing a new valid value.

Formal Syntax & Structure

rust syntaxrust
unsafe { slot.assume_init_drop() }

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::mem::MaybeUninit;    let mut slot = MaybeUninit::new(String::from("first"));    // SAFETY: valid initialized String, not previously moved or dropped.    unsafe { slot.assume_init_drop(); }    slot.write(String::from("second"));    // SAFETY: replacement is a new initialized String.    let value = unsafe { slot.assume_init() };    println!("{value}");}
Rust Reference