Arc in Rust
Atomic Reference Counting provides reference counting with a guarantee that any increment or decrement operation is atomic, meaning no two threads can simultaneously mutate the count.
This allows the reference-counted pointer to be safely shared and sent across threads.
std::sync::Arc: Arc stands for Atomically Reference Counted; it's the same as Rc, with a guarantee that any increment/decrement operation on Arc is atomic, i.e., no two threads can simultaneously mutate the count.
To get the count of references, use std::sync::Arc::strong_count and std::sync::Arc::get_mut to get a mutable reference.
use std::sync::Arc;
fn main() {
let mut num = Arc::new(45);
if let Some(val) = Arc::get_mut(&mut num) {
*val = 42;
}
println!("{num:?}");
}
