std-type

Rust RefMut guard

RefMut represents a runtime exclusive borrow. Its mutable dereference permits operations on the payload. A guard may be passed to a helper that owns it, but a second conflicting borrow is not allowed while it remains active. Expected output (not executed): 12 Why: The helper owns the guard and releases it on return. Common mistake: Treating RefMut as a Copy value.

Formal Syntax & Structure

rust syntaxrust
RefMut<'a, T>

Working Example

Example Snippetrust
use std::cell::{RefCell, RefMut};fn increase(mut n: RefMut<'_, i32>) { *n += 2; }fn main() {    let n = RefCell::new(10);    increase(n.borrow_mut());    println!("{}", *n.borrow());}
Rust Reference