In the definition of lifetime_things
, the lifetime of 'b
is longer than 'a
, but actually when I call this function, x1
is longer than y1
, but this can compile successfully:
//here you could see 'b:'a means, the lifetime of b should be larger than a,
fn lifetime_things<'a,'b:'a>(x:&'a String, y:&'b String) ->&'a String {
if x.len() > y.len() {
&x
} else {
&y
}
}
fn main() {
let z: i32;
let x = String::from("1");
let x1=&x;
{
let y = String::from("2");
let y1=&y;
println!("{}",lifetime_things(x1,y1));
}
}
But here you could see the lifetime of x1
should be larger than y1
so why can this compile successfully as well?