Why is it allowed to annotate a reference with a lifetime that does not correspond to the referenced resource?
In this example, x2 references the resource r2 that has a smaller lifetime than 'a, which is the lifetime of the resource r1.
#[derive(Debug)]
struct Foo {
f : i32
}
#[allow(unused_variables)]
fn m2<'a>(x1: &'a Foo, x2: &'a Foo) -> &'a Foo {
x1
}
fn m1<'a>(x1: &'a Foo) {
let r2 = Foo { f : 77 };
let t = m2(&x1, &r2);
println!("t: {:?}", t);
}
fn main() {
let r1 = Foo { f : 33 };
m1(&r1);
}
I would expect the compiler to forbid me from annotating the reference x2 with an already defined lifetime that does not correspond to the resource that it is referencing to.
#[derive(Debug)]
struct Foo {
f : i32
}
#[allow(unused_variables)]
fn m2<'a, 'b>(x1: &'a Foo, x2: &'b Foo) -> &'a Foo {
x1
}
fn m1<'a>(x1: &'a Foo) {
let r2 = Foo { f : 77 };
let t = m2(&x1, &r2);
println!("t: {:?}", t);
}
fn main() {
let r1 = Foo { f : 33 };
m1(&r1);
}
Could you tell me what I am missing please?