Code (also available on playground):
use std::sync::Mutex;
fn main() {
let a = Mutex::new(7);
let mut a = a.lock().unwrap();
a += 1;
}
Error:
error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, {integer}>`
--> src/main.rs:6:5
|
6 | a += 1;
| -^^^^^
| |
| cannot use `+=` on type `std::sync::MutexGuard<'_, {integer}>`
Okay, but why cannot += be applied to Mutexuard<'_, {integer}>? And what can we do about it? :)
I'd prefer if the error message included something like:
help: consider dereferencing the left-hand side: `*a += 1`
Such a help message could be printed whenever the LHS of an assignment must be dereferenced. The same applies to situations in which we must dereference more than once, e.g **a.
Code (also available on playground):
Error:
Okay, but why cannot
+=be applied toMutexuard<'_, {integer}>? And what can we do about it? :)I'd prefer if the error message included something like:
Such a help message could be printed whenever the LHS of an assignment must be dereferenced. The same applies to situations in which we must dereference more than once, e.g
**a.