std-function
Rust mem::replace(): exchange an owned value
replace stores a new value through a mutable reference and returns the old value by ownership. Neither value is destroyed by the replacement operation itself. Unlike take, it does not require Default. Expected output (not executed): queued -> running Why: old owns the former status while status owns the replacement. Common mistake: Using normal assignment when you need to retain the previous owned value.
Formal Syntax & Structure
rust syntaxrust
pub const fn replace<T>(dest: &mut T, src: T) -> TWorking Example
Example Snippetrust
use std::mem;fn main() { let mut status = String::from("queued"); let old = mem::replace(&mut status, String::from("running")); println!("{old} -> {status}");}