`Arc`
Arc<T> is an atomically reference-counted pointer. Cloning an Arc increments a shared count and gives another owner of the same T. The T drops when the last Arc drops.
Detailed Explanation
Rc is single-threaded shared ownership. Arc is the same idea with atomic counters, so clones may cross threads. let a = Arc::new(config); let b = Arc::clone(&a); is two owners, one Config. Cloning is O(1): bump a counter. It is not a deep copy of Config.
Arc<T> only gives shared access: &T. You cannot get &mut T unless T has interior mutability (Mutex, RwLock, AtomicU64) or you have the last owner (Arc::get_mut). That is why the pair you actually spawn with is Arc<Mutex<T>> or Arc<RwLock<T>>, not Arc<T> alone, when workers must write.
Arc is Send + Sync when T is Send + Sync. Arc<RefCell<T>> is not Send. Arc<Mutex<T>> is. Weak<T> is a non-owning handle that does not keep T alive; upgrade returns Option<Arc<T>>. Use Weak to break cycles, the same as with Rc.
The atomic increment and decrement have a cost. On a hot single-threaded path, Rc is cheaper. On a path that never shares, own the value. Arc is the tool you reach for when a thread or a Tokio task needs to keep the value alive after the creator has moved on.
Arc<[T]> and Arc<str> are fat-pointer-friendly owned slices without the extra capacity word of a Vec. They show up as cheap-to-clone immutable buffers.
When you reach for Arc: shared ownership across threads or tasks. When you reach for Rc: shared ownership on one thread. When you reach for clone of the inner T: you actually need a deep copy, not a second owner.
Drop order of Arc clones is not a lock. If the last clone drops on a thread that is not allowed to free the inner type (some FFI allocators, some TLS-bound values), you have a design problem Arc will not solve. Use a custom destructor, a cleanup thread, or keep an owner on the creating thread. For ordinary Send data this does not arise.
Code Examples
use std::sync::Arc;
use std::thread;
fn main() {
let name = Arc::new(String::from("ada"));
let name2 = Arc::clone(&name);
let h = thread::spawn(move || {
format!("hello {name2}")
});
println!("{}", h.join().unwrap()); // prints: hello ada
println!("{}", name.len()); // prints: 3 — original Arc still usable
}Common Gotchas
`Arc<T>` does not make `T` mutable
You need `Mutex`, `RwLock`, or atomics inside. `Arc<T>` alone is read-only sharing.
Cycles leak
Two `Arc`s that point at each other never reach a count of zero. Use `Weak` for the back edge.