Arc (Rust)

In Rust, Arc<T> = Atomic Reference Counted pointer is a thread-safe way to share ownership of some data T across multiple parts of your program (especially across threads).

What’s actually happening when you do this:

 
let v = vec![1,2,3];        // `v` is a stack variable that OWNS heap memory
let a = std::sync::Arc::new(v);
let b = std::sync::Arc::clone(&a);
  • v (the variable) lives on the stack, but the vector’s buffer already lives on the heap.
  • Arc::new(v) moves ownership of the Vec into an Arc allocation (also on the heap).
  • After that, a and b are just small handles (pointers + refcount) you can move to threads.

When do I need Arc::clone?

You only need Arc::clone(&a) when you need another owner — typically because you’re going to move one copy somewhere else (like into a thread/task) but you still want to keep using the original a too.