std-method

Rust Arc::get_mut

When there are no other strong or weak handles, Arc can grant mutable access directly. The safe method performs the uniqueness check; observing a count alone is not a replacement for this method in concurrent code. Expected output (not executed): true [1, 2] Why: Unique access works first; the extra owner then prevents it. Common mistake: Using an observed strong count as a synchronization mechanism.

Formal Syntax & Structure

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

Working Example

Example Snippetrust
use std::sync::Arc;fn main() {    let mut settings = Arc::new(vec![1]);    Arc::get_mut(&mut settings).unwrap().push(2);    let second = Arc::clone(&settings);    println!("{}", Arc::get_mut(&mut settings).is_none());    drop(second);    println!("{:?}", settings);}
Rust Reference