let mut x = 1;
let a = &mut x;
let b = &mut *a;
*a = 2; // error. b borrows a
*b = 3; // it works! (only without " *a = 2 ")
let mut x = 1;
let b;
{
let a = &mut x;
b = &mut *a;
} // a drops here
*b = 2; // it works!
I'm having hard time getting what &*a
means, in the point of lifetime
.
I don't know how *
operator is related to lifetimes of variables.
It seems like b is borrowing x and also a, so that not only x(which is *a) cannot be moved or modified but also a
cannot be used.
The error message of compiler is : b borrowing a.
So, I ran the second code.
In my understanding, borrowed data cannot be reassigned or moved, or dropped.
I deliberately made a
to drop before b
, to make sure that a
's lifetime should be longer than b
's.
However, second code still works.
So, how can I understand undergoing lifetimes associated with &mut *a
?