std-module
std::sync: coordinate shared state
Shared ownership and shared mutation solve different problems. A scoped thread can borrow a lock directly, while an Arc can share ownership beyond a scope. Choose the simplest mechanism that fits the lifetime and invariant. Expected stdout (not executed): 1 Common mistake: Assuming sharing ownership alone protects mutable state.
Formal Syntax & Structure
rust syntaxrust
use std::sync::{Arc, Mutex};Working Example
Example Snippetrust
fn main() { use std::sync::Mutex; let count = Mutex::new(0); std::thread::scope(|s| { s.spawn(|| *count.lock().unwrap() += 1); }); println!("{}", count.into_inner().unwrap());}