Send and Sync
Two auto traits govern threads:
Send: a value can be moved to another thread.Sync: a value can be shared by reference across threads.
fn assert_send<T: Send>() {}
Most types are Send and Sync automatically. Smart pointers like Rc are not Send: use Arc instead.
Why it works: these traits let Rust guarantee thread safety.
Mutex
A Mutex serializes access:
let counter = Mutex::new(0);
*counter.lock().unwrap() += 1;
lock() returns a guard. Holding the guard means no other thread can access the data. unwrap panics if another thread panicked while holding the lock.