Lifetime annotation does not change how long a reference lives. But it can affect how people write code. Both line 6 & 7 does not compile with the below restricted lifetime version of first2() function. But line 6 compiles with the relaxation version of the first() function, and line 7 does not compile with it.
It turns out the order of the arguments to function call matters : ) Can the language mandates restricted lifetime version by default, so the order will not matter?
Is it possible to write overloaded function based on lifetime parameter? So the two function can share the same name.
fn main() {
let s1 = String::from("s1");
let ret;
{
let s2 = String::from("s2");
ret = first(s1.as_str(), s2.as_str()); //line 6
// ret = first(s2.as_str(), s1.as_str()); //line 7, Err on relaxation ver
}
println!("ret: {}", ret);
}
//relaxation lifetime version
//connecting some parameter references and return ref.
fn first<'a>(s1: &'a str, s2: &str) -> &'a str {
s1
}
//restricted lifetime version,
//connecting all parameter references and return ref.
fn first2<'a>(s1: &'a str, s2: &'a str) -> &'a str {
s1
}