Complete the code to specify a lifetime parameter for the function.
fn longest<'a>(x: &'a str, y: &str) -> &'a str { if x.len() > y.len() { x } else { y } }
The lifetime parameter 'a is used to link the lifetime of the input x and the output reference.
Complete the code to declare the lifetime for both input parameters.
fn longest<'a>(x: &'a str, y: [1]) -> &'a str { if x.len() > y.len() { x } else { y } }
&str without a lifetime.&'b str.&'static str which is too restrictive.Both input parameters must have the same lifetime 'a to ensure the output lifetime is valid.
Fix the error in the function signature by adding the correct lifetime to the return type.
fn first_word<'a>(s: &'a str) -> [1] { let bytes = s.as_bytes(); for (i, &item) in bytes.iter().enumerate() { if item == b' ' { return &s[0..i]; } } &s[..] }
String instead of a reference.&'static str incorrectly.The return type must have the same lifetime as the input reference to ensure safety.
Fill both blanks to correctly declare lifetimes for the function parameters and return type.
fn choose_first<'a, 'b>(x: &'a str, y: &'b str) -> [1] { x } fn choose_second<'a, 'b>(x: &'a str, y: &'b str) -> [2] { y }
&str without lifetime annotations.String instead of references.The return type must match the lifetime of the reference returned from the function.
Fill all three blanks to correctly declare lifetimes and return a reference with the correct lifetime.
fn longest_with_announcement<'a>(x: &'a str, y: &'a str, ann: &str) -> [1] { println!("Announcement: {}", ann); if x.len() > y.len() { [2] } else { [3] } }
String instead of references.The return type lifetime must match the lifetime of the returned reference. The function returns either x or y, so the lifetime must be declared accordingly.