Arc and shared ownership
Quick answer
Arc<T> is an atomically reference-counted pointer. It supports shared ownership across threads when the contained type satisfies the required thread-safety bounds. It does not automatically make arbitrary data safe to mutate concurrently.
Ownership versus synchronization
Cloning Arc makes another owner of the same data. To change shared data across threads, combine it with a suitable synchronization mechanism. A mutex provides exclusive access through a lock guard; handle possible poisoning appropriately. The thread APIs here preview the dedicated concurrency chapter.
use std::{sync::Arc, thread};fn main() { let data = Arc::new(vec![2, 4, 6]); let other = Arc::clone(&data); let worker = thread::spawn(move || other.iter().sum::<i32>()); println!("sum: {}", worker.join().unwrap()); println!("length: {}", data.len()); }Expected stdout (not executed):
sum: 12length: 3Join waits for the worker result; the main owner remains.
use std::{sync::{Arc, Mutex}, thread};fn main() { let count = Arc::new(Mutex::new(0)); let other = Arc::clone(&count); thread::spawn(move || { *other.lock().unwrap() += 1; }).join().unwrap(); println!("{}", *count.lock().unwrap()); }Expected stdout (not executed):
1The guard holds the mutex during the increment. unwrap is used only for this controlled example.
Common mistake
Arc<RefCell<T>> does not acquire the thread-safety properties of Arc<Mutex<T>>.
Try it yourself
Explain why cloning Arc and locking Mutex address different problems.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Arc and shared ownership — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
Workers share an Arc<Vec<u32>> and need to append safely. What is missing?
Arc and shared ownership — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{sync::Arc, thread}; fn main() { let data = Arc::new(vec![2, 4, 6]); let other = Arc::clone(&data); let worker = thread::spawn(move || other.iter().sum::<i32>()); println!("sum: {}", worker.join().unwrap()); println!("length: {}", data.len()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Arc and shared ownership — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{sync::{Arc, Mutex}, thread}; fn main() { let count = Arc::new(Mutex::new(0)); let other = Arc::clone(&count); thread::spawn(move || { *other.lock().unwrap() += 1; }).join().unwrap(); println!("{}", *count.lock().unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.