How does "{:?}" read value wrapped inside Arc<Mutex> without acquiring a lock?

The whole point of the lock is specifically so that others can't see any mutations that might be happening concurrently. You have to lock the mutex to ensure nobody else is currently mutating it. There's no way to read the value without locking it, and the mutex would arguably not be doing its job if you could.

You can use RwLock if you want multiple concurrent readers, but they will all still all have to lock it to ensure there are no concurrent writers.

The debug formatter prints the value by attempting to lock it first.

2 Likes