Why I cannot dereference &&mut to mutate underlying value?
let mut x = 5;
let y = &mut x;
let z = &y; // &&mut reference
**z = 10;
compile error,
error[E0594]: cannot assign to `**z` which is behind a `&` reference
--> src/main.rs:15:5
|
15 | **z = 10;
| ^^^^^^^^ cannot assign
error: aborting due to previous error
I don't think E0594 explains it well.
I tried with C++, the equivalent code is,
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* const y = &x;
typedef int* const y_ptr;
const y_ptr* const z = &y;
cout << **z << endl;
**z = 11;
cout << **z << endl;
return 0;
}
and the output is
10
11
edited at 03/01/2020 9:30 pm
It, &&mut
, is also mentioned in reasoning behind match ergonomics,
This is because an
&mut
inside of a&
is still a shared reference, and thus cannot be used to mutate the underlying value.