0

My code is:

fn main() {
    let mut foo = 1024;
    let bar = &mut foo;
    let baz:&mut i32 = bar;
    let qux = bar;
    assert_eq!(baz,qux);
}

Of course, this code has errors. However, what puzzles me is the error message:

error[E0505]: cannot move out of `bar` because it is borrowed
 --> main.rs:5:15
  |
4 |     let baz:&mut i32 = bar;
  |                        --- borrow of `*bar` occurs here
5 |     let qux = bar;
  |               ^^^ move out of `bar` occurs here
6 |     assert_eq!(baz,qux);
  |     -------------------- borrow later used here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0505`.

The fourth line print borrow of '*bar', while the fifth line print move out of 'bar'. Why is that?(My rustc version is 1.55.0).

ZyS
  • 71
  • 2
  • See also [issue 25899](https://github.com/rust-lang/rust/issues/25899) (which stems from the SO question above), and the various issues linking back to it. – Masklinn Oct 02 '21 at 08:57

0 Answers0