In one tutorial, I saw something like this:
let mut x = 2; let ref1 = &x; let ref2 = &mut x;
This doesn’t work because Rust doesn’t allow
mutable and immutable reference to the same thing in the same scope.
So far so good. But then I started to think about binding.
What if, instead of binding to ref2,
I simply try to make a re-bind of ref1, like this:
let mut x = 2; // no changes here
let ref1 = &x; // no changes here
let ref1 = &mut x; // this doesn’t work, but this completely confused me
I understand I can’t have both immutable and mutable borrow.
But I was somehow expecting re-binding of the ref1 to be more
important than the fact it was holding a reference.
Or (maybe this is the key to understanding): the fact that there was a borrow.
I mean, ref1 is the only borrower.
But this means that ref1 cannot rebind (using let) and borrow x as mutable,
as long as “there is a borrow” - even if one who borrows is the same,
one and only ref1?
This is a bit confusing - or maybe I just need another way to think about this…