Comment

Comment by legerdemain

You can 100% have multiple mutable references to the same variable in the same scope, without the first one getting dropped. For example:

   fn main() {
      let mut s = String::from("s");
      let mut1: &mut _ = &mut s;
      let mut2: &mut _ = &mut *mut1;
      *mut2 = String::from("t");
      println!("using mut2: {mut2}");
      println!("using mut1: {mut1}");
   }
Some people use a mental model of reference lifetimes that allows "discontinuous lifetimes" where the valid region has holes. I don't think that's how the compiler models reborrowing, and even under that model, `mut1` is created before `mut2` and gets dropped after `mut2`.
parent legerdemain 2 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton