I am trying to wrap my head around behaviour with the "move semantics" when dealing with references and looking for confirmation or correction.
So when I try to run the following code
fn main() {
let a : &mut Box<usize> = &mut Box::new(0);
let b : &mut Box<usize> = a;
let c : &Box<usize> = a;
let d : &Box<usize> = &a;
}
I get the following two errors
error[E0502]: cannot borrow `*a` as immutable because it is also borrowed as mutable
--> src/main.rs:6:27
|
4 | let b : &mut Box<usize> = a;
| - mutable borrow occurs here
5 |
6 | let c : &Box<usize> = a;
| ^ immutable borrow occurs here
...
9 | }
| - mutable borrow ends here
error[E0502]: cannot borrow `a` as immutable because `*a` is also borrowed as mutable
--> src/main.rs:8:28
|
4 | let b : &mut Box<usize> = a;
| - mutable borrow occurs here
...
8 | let d : &Box<usize> = &a;
| ^ immutable borrow occurs here
9 | }
| - mutable borrow ends here
My understanding is that when I try to "move" a reference out of a binding(in this case, a
), instead of "moving" the references between bindings, it becomes borrowing
-
*a
(in the case oflet b ...
andlet c ...
) - or
a
(in the case oflet d ...
)
And the normal reference rules apply regarding *a
and a
afterwards, i.e.
- cannot borrow
*a
ora
mutably if*a
ora
was already borrowed mutably or immutably - cannot borrow
*a
ora
immutably if*a
ora
was already borrowed mutably - can borrow immutably both
*a
anda
if*a
ora
was borrowed immutably previously
Is my understanding correct?
Thanks for your time!