Hint: Declare lifetime before fn name and use on references [OK]
Common Mistakes:
Putting lifetime after & instead of before type
Forgetting to declare lifetime before function name
Using lifetime on non-reference types
3.
What will this Rust code print?
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() { x } else { y }
}
fn main() {
let s1 = String::from("apple");
let s2 = "banana";
let result = longest(&s1, s2);
println!("{}", result);
}
medium
A. apple
B. banana
C. Compilation error due to lifetimes
D. Runtime error due to invalid reference
Solution
Step 1: Understand function longest behavior
It returns the longer string slice between x and y.
Step 2: Compare lengths of "apple" and "banana"
"apple" has 5 letters, "banana" has 6, so it returns "banana".
Final Answer:
banana -> Option B
Quick Check:
Longest string = banana [OK]
Hint: Compare string lengths carefully to find longest [OK]
Common Mistakes:
Assuming s1 is longer without counting
Thinking lifetimes cause compile error here
Confusing references with ownership causing runtime error
4.
Identify the error in this Rust code related to lifetimes:
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s
}
fn main() {
let word;
{
let s = String::from("hello world");
word = first_word(&s);
}
println!("{}", word);
}
medium
A. Error: cannot borrow s as mutable
B. Error: missing lifetime annotation in function signature
C. Error: returned reference may outlive borrowed data
D. No error, code runs fine
Solution
Step 1: Analyze lifetime of variable s
Variable s is created inside inner block and dropped after it ends.
Step 2: Check lifetime of returned reference word
word holds reference to s, but s is dropped before println!, causing dangling reference.
Final Answer:
Error: returned reference may outlive borrowed data -> Option C
Quick Check:
Dangling reference error = Error: returned reference may outlive borrowed data [OK]
Hint: References must not outlive their data scope [OK]
Common Mistakes:
Ignoring scope of s and thinking code is safe
Assuming missing lifetime annotation causes error here
Confusing mutable borrow errors with lifetime errors
5.
You want to write a function that returns a reference to the longer string slice between two inputs, but one input is owned String and the other is a string slice &str. How should you use lifetimes to make this safe and correct?
hard
A. Convert the owned String to a slice and use a single lifetime for both inputs and output
B. Return the owned String directly without lifetimes
C. Use different lifetimes for each input and output to avoid conflicts
D. Clone both inputs and return an owned String without references
Solution
Step 1: Understand input types and lifetimes
Owned String must be converted to a string slice (&str) to use lifetimes uniformly.
Step 2: Use one lifetime parameter for both inputs and output
This ensures the returned reference is valid as long as both inputs are valid.
Final Answer:
Convert the owned String to a slice and use a single lifetime for both inputs and output -> Option A
Quick Check:
Single lifetime with slices = Convert the owned String to a slice and use a single lifetime for both inputs and output [OK]
Hint: Use &str slices and one lifetime for safe references [OK]
Common Mistakes:
Returning owned String without references when references expected
Using multiple lifetimes causing borrow conflicts
Forgetting to convert String to &str before comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.