I\'m learning Rust and have no experience with threads. I\'m going through the Rustlings course and I\'ve solved the threads1.rs
exercise, but I don\'t understand w
status_shared
is of type MutexGuard
. MutexGuard
implements the DerefMut and Deref traits, with a deref target of T
(the type which is stored inside the Mutex - JobStatus
in your case.
When you use behind a .
behind an object the rust compiler will automatically try to deref it into something where the requested operation can be performed. Therefore the explicit dereferencing is not necessary here. This behavior is described in the Rust book in the Deref chapter
Arc<T> automatically dereferences via the Deref trait.
References:
As @Matthias247 said, dereferencing is automatic when behind a .
Additionally, your attempt at explicit dereference fails because of operator precedence: *status_shared.jobs_completed
is equivalent to *(status_shared.jobs_completed)
so it attempts to dereference an u32
and fails, but you want (*status_shared).jobs_completed
in order to dereference the Arc<JobStatus>
.