std-module

Rust std::mem module

The mem module includes safe value-moving operations and tools for inspecting representation, as well as APIs for manual lifetime management. Prefer safe operations such as replace when they express the intended ownership transfer. Expected stdout (not executed): old -> new Common mistake: Assuming every operation in std::mem is unsafe.

Formal Syntax & Structure

rust syntaxrust
use std::mem;

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    let mut state = String::from("old");    let previous = std::mem::replace(&mut state, String::from("new"));    println!("{previous} -> {state}");}
Rust Reference