Why does shadowing not release a borrowed reference?

Viewed 243

It seems that shadowing a variable does not release the borrowed reference it holds. The following code does not compile:

fn main() {
    let mut a = 40;
    let r1 = &mut a;
    let r1 = "shadowed";
    let r2 = &mut a;
}

With the error message:

error[E0499]: cannot borrow `a` as mutable more than once at a time
 --> src/main.rs:5:19
  |
3 |     let r1 = &mut a;
  |                   - first mutable borrow occurs here
4 |     let r1 = "shadowed";
5 |     let r2 = &mut a;
  |                   ^ second mutable borrow occurs here
6 | }
  | - first borrow ends here

I would expect the code to compile because the first reference r1 is shadowed before borrowing the second reference r2. Obviously, the first borrow lives until the end of the block although it is no longer accessible after line 4. Why is that the case?

2 Answers
Related