0

Why does this compile?

fn main() {
  let mut x = vec![1, 2, 3];
  let y = &mut x;
  let z: &mut _ = y;
  let xx = y;
}

when z reborrow x in this statement let z: &mut _ = y; there already exist a mutable reference to x in y. Doesnt this violate rust principle?

raj
  • 5,989
  • 7
  • 30
  • 62
  • But when z which is a mutable reference to x is being defined, y is still in scope and not dropped bcz it is used in the next line. – raj Sep 25 '21 at 16:08
  • 2
    If `z` were initialized with `&mut x`, it would conflict with `y` and cause an error, however `z` is a reborrow from `y` and because of non-lexical-lifetimes `z`'s lifetime ends in the same line as its created so the use of `y` is no longer in conflict. – kmdreko Sep 25 '21 at 17:57

0 Answers0