Complete the code to declare a reference with a lifetime.
fn print_str<[1]>(s: &[1] str) { println!("{}", s); }
mut or ref instead of a lifetime parameter.The 'a syntax declares a lifetime parameter for the reference &'a str.
Complete the function signature to specify that the returned reference has the same lifetime as the input.
fn first_char<[1]>(s: &[1] str) -> &[1] str { &s[0..1] }
The lifetime 'a is used to link the input and output lifetimes, ensuring the returned reference is valid as long as the input.
Fix the error by adding the correct lifetime annotation to the struct.
struct Holder<[1]> { value: &[1] str }
The struct needs a lifetime parameter 'a to specify how long the reference inside it is valid.
Fill both blanks to define a function that returns the longer of two string slices with correct lifetimes.
fn longer<'[1]>(x: &'[1] str, y: &'[1] str) -> &'[2] str { if x.len() > y.len() { x } else { y } }
The lifetime 'a is used consistently to tie the input references and the output reference together.
Fill all three blanks to define a struct with two references having different lifetimes.
struct Pair<'[1], '[2]> { first: &'[1] str, second: &'[2] str }
The struct uses two different lifetime parameters 'a and 'b for its two references.