Can someone explain why the second codeblock fails? I think this is somehow related to type auto deduction.
When I explicitely write out the types it compiles
{
let mut ref1: &mut i32 = &mut 4;
let mut ref2: &mut &mut i32 = &mut ref1;
let mut ref3: &mut i32 = *ref2; //perfectly fine
println!("{:?}", ref3);
}
// this does not compile
{
let mut ref1 = &mut 4;
let mut ref2 = &mut ref1;
let mut ref3 = *ref2; // this causes 'cannot move out of `*ref2` which is behind a mutable reference'
println!("{:?}", ref3);
}
EDIT
I think the same issue here
let mut ref1: &mut &mut i32 = &mut &mut 4;
let mut x = *ref1; // same compile error
while this compiles
let mut ref1: &mut &mut i32 = &mut &mut 4;
let mut x: &mut i32 = *ref1;
so it seems 're-borrowing' a mutable inner reference mutably is okay, while moving it out is not okay, which makes sense
thank you: @kmdreko