std-type

Rust Arc<T>

Arc is a reference-counted owner whose bookkeeping supports sharing across threads when the payload meets the necessary thread-safety bounds. For the default allocator, T must be Send and Sync for Arc<T> to be both. Atomic bookkeeping does not make arbitrary payload mutation safe. Expected output (not executed): 3 15 Why: The thread receives an owning handle, not a deep copy of the vector. Joining makes the printed order deterministic. Common mistake: Replacing Rc<RefCell<T>> with Arc<RefCell<T>> and expecting it to become thread-safe.

Formal Syntax & Structure

rust syntaxrust
Arc::new(value)

Working Example

Example Snippetrust
use std::{sync::Arc, thread};fn main() {    let config = Arc::new(vec![2, 5, 8]);    let worker_config = Arc::clone(&config);    let job = thread::spawn(move || worker_config.iter().sum::<i32>());    println!("{} {}", config.len(), job.join().unwrap());}
Rust Reference