Consider the following:
struct Str<'a> {
s: &'a str,
}
fn f1<'a>(_: &'a mut Str<'a>) {}
fn f2<'a, 'b>(_: &'a mut Str<'b>) {}
fn main() {
let s = "hello".to_string();
let mut a = Str {
s: &s,
};
f1(&mut a);
// f2(&mut a);
let t: &Str = &a;
}
f2 uses two different lifetimes, as it would when I elided them, which works fine.
At this point, I thought that the lifetime 'a refers to the lifetime of &mut a, and 'b refers to the lifetime of &s.
And then I wrote f1 which uses a single lifetime parameter, suspecting that lifetime 'a would refer to the shorter of the lifetime of &mut a and the lifetime of &s.
However, this f1 fails with the following error:
error[E0502]: cannot borrow `a` as immutable because it is also borrowed as mutable
--> src/main.rs:21:19
|
18 | f1(&mut a);
| ------ mutable borrow occurs here
...
21 | let t: &Str = &a;
| ^^
| |
| immutable borrow occurs here
| mutable borrow later used here
The error confuses me: Why is a still borrowed as mutable after calling f1?
Why does this fail, and what does the error message supposed to mean?