(simplified example and solution 8 posts down)
Hi all, regarding lifetimes:
The following code compiles fine:
struct Foo;
struct Link<'a> {
_link: &'a Foo,
}
fn store<'a>(_x: &Link<'a>, _y: &'a Foo) {
}
fn main() {
let a = Foo;
let x = &mut Link { _link: &a };
{
let b = Foo;
store(x, &b);
}
}
But if I change store
's definition to take a mutable reference to a Link
:
fn store<'a>(_x: &mut Link<'a>, _y: &'a Foo) {
}
I get a compile error 'b does not live long enough'.
In case of an immutable reference, Rust does not complain about b
going out of scope before x
. In case of a mutable reference, it does.
What gives?
Does store
preemptively "refuse" to take a &mut
, because it "might" mutate the reference inside the Link
(to &b
, which has insufficient lifetime), even though it doesn't?