std-method

Rust Rc::get_mut

This API provides mutable access only when the allocation has no other strong or weak handles. It does not duplicate the payload. Prefer matching the Option when uniqueness is not guaranteed. Expected output (not executed): true 22 Why: The weak handle also prevents get_mut from granting exclusive access. Common mistake: Checking only the strong count to assume get_mut must succeed.

Formal Syntax & Structure

rust syntaxrust
Rc::get_mut(&mut handle) -> Option<&mut T>

Working Example

Example Snippetrust
use std::rc::Rc;fn main() {    let mut data = Rc::new(20);    if let Some(n) = Rc::get_mut(&mut data) { *n += 2; }    let observer = Rc::downgrade(&data);    println!("{}", Rc::get_mut(&mut data).is_none());    drop(observer);    println!("{}", *Rc::get_mut(&mut data).unwrap());}
Rust Reference