Arc and Mutex: shared state across threads
`Arc<T>` is `Rc<T>` with an atomic count, so it can cross thread boundaries. `Mutex<T>` guards the data inside it: you cannot read or write without locking first, which makes the lock impossible to forget.
`lock()` returns a guard. The lock is released when the guard is dropped, so scoping matters — hold it for as little as possible.
The `Send` and `Sync` marker traits are what the compiler checks here. A type that is not safe to share simply will not compile into this shape, which is where 'fearless concurrency' comes from.
Run it and change it
The editor below is live: edit anything and the real compiler output updates by itself.
Contrast AAA · 16.6:1
15px