Hey. I am a beginner in Rust.
I don't understand, why we need lifetimes in functions if we still borrow arguments?
Regardless of which of the arguments is returned, or no any of them, we can still use any argument that we passed to the functions.
fn return_diff_lifetime_1 <'a,'b> (x: &'a str, y: &'b str) -> &'a str { x }
fn return_diff_lifetime_2 <'a,'b> (x: &'a str, y: &'b str) -> &'b str { y }
fn return_same_lifetime_1 <'a> (x: &'a str, y: &'a str) -> &'a str { x }
fn return_same_lifetime_2 <'a> (x: &'a str, y: &'a str) -> &str { x }
fn return_not_an_argument <'a> (x: &'a str, y: &'a str) -> &str { "not an arg" }
let (x, y) = ("test1", "test2");
return_diff_lifetime_1 (x, y);
return_diff_lifetime_2 (x, y);
return_same_lifetime_1 (x, y);
return_same_lifetime_2 (x, y);
return_not_an_argument (x, y);
println!("{:?}", x); // "test1"
println!("{:?}", y); // "test2"
Also, the removal of lifetimes in the example from the book will not affect anything
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
It looks like we don't need lifetimes in function signature because it doesn't affect anything