// Created on savesnippets.com · https://savesnippets.com/Yboo6o2DEkJ3yF use std::rc::Rc; use std::sync::Arc; fn main() { // Single-threaded shared ownership let shared = Rc::new(vec![1, 2, 3]); let a = Rc::clone(&shared); // bumps refcount, no deep copy let b = Rc::clone(&shared); println!("count={}", Rc::strong_count(&shared)); // 3 println!("{a:?} {b:?}"); // When the last Rc is dropped, the Vec is freed. // Multi-threaded — Arc is atomic refcounted let big = Arc::new(vec![0u8; 1024 * 1024]); let handles: Vec<_> = (0..4).map(|i| { let chunk = Arc::clone(&big); std::thread::spawn(move || chunk.iter().take(10 + i).count()) }).collect(); for h in handles { println!("counted {}", h.join().unwrap()); } }