Sub topic 11
Reborrowing a mutable reference
This chapter assumes you know how &mut T works and the rule of many readers or one writer. Here you will see why you can pass the same &mut reference to several functions in a row, even though a &mut reference cannot be copied.
The puzzle
A &mut T is not Copy. Passing it by value should therefore move it, and the next use should fail. Yet this compiles:
fn push_one(v: &mut Vec<i32>) {
v.push(1);
}
fn main() {
let mut v = Vec::new();
let r = &mut v;
push_one(r);
push_one(r);
println!("{:?}", r);
}The compiler does not move r into the call. Because the parameter type is known to be &mut Vec<i32>, it inserts a reborrow. It treats push_one(r) as push_one(&mut *r).
What a reborrow is
A reborrow creates a new reference to the data behind an existing reference. You write it as &mut *r. Read it from the inside out:
*ris the value thatrpoints to.&mutborrows that value again, mutably.
The new reference is temporary. While it is alive, the original r is frozen and cannot be used. When the new reference is last used, r works again.
Writing a reborrow by hand
You can write the reborrow yourself to see the rule at work:
fn main() {
let mut v = vec![1];
let r = &mut v;
let inner = &mut *r;
inner.push(2);
r.push(3);
println!("{:?}", r);
}This compiles because inner is last used before r.push(3). Non-lexical lifetimes end the reborrow at that point. If you swap the order, the code no longer compiles:
fn main() {
let mut v = vec![1];
let r = &mut v;
let inner = &mut *r;
r.push(3);
inner.push(2);
}The compiler reports E0499, "cannot borrow *r as mutable more than once at a time". This is the one-writer rule applied to the reborrow. The two references would overlap, and both could write.
Why this is safe
A reborrow never creates a second independent writer. The borrow checker records that inner is derived from r and blocks r for as long as inner lives. At any moment, only one reference can reach the data.
Reborrowing also works for shared access. Writing &*r gives you a shared reference from a &mut reference. While it lives, r can be read but not written.
When the implicit reborrow does not happen
The compiler reborrows automatically only when it knows the target type is a reference. With a generic parameter, it moves the reference instead:
fn consume<T>(_x: T) {}
fn main() {
let mut n = 0;
let r = &mut n;
consume(r);
consume(r);
}This does not compile. The second call fails with E0382, "use of moved value: r". The fix is to reborrow explicitly: consume(&mut *r).
Summary of the rules
| Expression | Result | While it is alive |
|---|---|---|
| `&mut *r` | New mutable reference | `r` cannot be used |
| `&*r` | New shared reference | `r` can be read, not written |
| `f(r)` with a `&mut T` parameter | Implicit `&mut *r` | `r` is usable after the call |
| `f(r)` with a generic `T` parameter | Move | `r` is gone |
Exercise
Why does push_one(r); push_one(r); compile even though &mut Vec<i32> is not Copy?
Show answer
Because the parameter type is a known reference type, the compiler reborrows r as &mut *r for each call instead of moving it. Each reborrow ends when the call returns, so r is usable again.
Exercise
In the second manual example, inner = &mut *r is created, then r.push(3) is called, then inner.push(2) is called. Which error does the compiler give, and why?
Show answer
E0499. The reborrow inner is still used after r.push(3), so the two mutable borrows of the same data overlap.