Rustlings thread exercise, why do I NOT dereference Mutex(Struct)?

后端 未结 3 2176
野趣味
野趣味 2021-01-22 15:39

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

3条回答
  •  庸人自扰
    2021-01-22 15:52

    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

提交回复
热议问题