The code wants to constrain x
and y
to the same lifetime and uses a function to do so. But the compiler does not error when y
is dropped. Does this mean lifetime bounds do not work between function arguments, irrespective of function return value?
static S: String = String::new();
// `x` and `y` have the same lifetime
fn foo<'a>(x: &'a String, y: &'a String) -> &'static String {
if x == y {
&S
} else {
&S
}
}
fn main() {
let z;
let x = &"x".to_string();
{
let y = &"y".to_string();
z = foo(x, y);
} // drops `y`
dbg!(x);
dbg!(z);
}