0
fn main() {
    let a = &mut 1;
    let b: &mut i32 = a;
    // let b = a;
    a;
}

The above works, but switching out let b line for the commented-out line below produces the following error:

error[E0382]: use of moved value: `a`
 --> src/junk.rs:5:5
  |
2 |     let a = &mut 1;
  |         - move occurs because `a` has type `&mut i32`, which does not implement the `Copy` trait
3 |     // let b: &mut i32 = a;
4 |     let b = a;
  |             - value moved here
5 |     a;
  |     ^ value used here after move

My questions are:

  1. What are the semantic differences between the two versions?
  2. Is there a difference in types between b in the two versions?
Jack Reilly
  • 463
  • 2
  • 8
  • 1
    When the target type is already known to be a mutable reference, the compiler will implicitly reborrow the mutable reference, i.e. the assignment is equivalent to `let b: &mut i32 = &mut *a;`. This is a frequent question – I'll look up a duplicate with a detailed answer for you. – Sven Marnach Oct 27 '22 at 11:39
  • Thanks so much @SvenMarnach for finding the duplicate and helping me with further keywords to learn more, much appreciated! – Jack Reilly Oct 27 '22 at 12:48

0 Answers0