How to Use Arc<T> for Thread-Safe Shared Ownership

Use Arc<T> to safely share immutable data between threads by cloning the Arc handle.

Use Arc<T> to share immutable data across threads by wrapping it in an Arc and cloning the handle for each thread.

use std::sync::Arc;
use std::thread;

fn main() {
    let data = Arc::new(vec![1, 2, 3]);
    let data_clone = Arc::clone(&data);

    thread::spawn(move || {
        println!("Thread 1: {:?}", data_clone);
    });

    println!("Main: {:?}", data);
}

For mutable shared state, wrap the Arc in a Mutex or RwLock.