2. Which of the following is the correct syntax to declare a function that takes a slice with a lifetime parameter?
fn process_slice(____) {}
easy
A. &'a [i32]
B. &'a [i32];
C. &[i32]
D. &[i32]'a
Solution
Step 1: Recall lifetime syntax for references
Lifetimes are written as &'a before the type to indicate the lifetime of the reference.
Step 2: Check each option
&'a [i32] correctly places &'a before the slice type [i32]. &[i32] misses lifetime, &'a [i32]; has an extra semicolon, &[i32]'a places lifetime incorrectly.
Final Answer:
&'a [i32] -> Option A
Quick Check:
Lifetime syntax = &'a [type] [OK]
Hint: Lifetimes go right after & before type [OK]
Common Mistakes:
Putting lifetime after the type
Adding extra semicolons inside parameters
Omitting lifetime when needed
3. What will be the output of this Rust code?
fn get_first<'a>(slice: &'a [i32]) -> &'a i32 {
&slice[0]
}
fn main() {
let numbers = vec![10, 20, 30];
let first = get_first(&numbers);
println!("{}", first);
}
medium
A. 20
B. 10
C. Compilation error due to lifetime mismatch
D. Runtime panic
Solution
Step 1: Understand the function behavior
The function returns a reference to the first element of the slice, which is 10.
Step 2: Check lifetime correctness and usage
The lifetime 'a ensures the returned reference is valid as long as the input slice is valid. The vector lives long enough, so no error.
Final Answer:
10 -> Option B
Quick Check:
Returned first element = 10 [OK]
Hint: Returned slice element matches first index [OK]
Common Mistakes:
Thinking lifetime causes compile error here
Confusing element values
Expecting runtime panic without reason
4. Identify the error in this Rust code snippet:
fn invalid<'a>() -> &'a [i32] {
let data = vec![1, 2, 3];
&data[..]
}
medium
A. Missing lifetime annotation on data
B. Syntax error in slice syntax
C. The function returns a reference to data that will be dropped
D. No error, code is correct
Solution
Step 1: Analyze the function return and data lifetime
The function returns a slice reference with lifetime 'a, but data is created inside the function and dropped when it ends.
Step 2: Understand why this causes an error
Returning a reference to local data is invalid because the data won't live long enough, causing a dangling reference.
Final Answer:
The function returns a reference to data that will be dropped -> Option C
Quick Check:
Returning local reference = error [OK]
Hint: Never return reference to local variable [OK]
Common Mistakes:
Assuming lifetime annotation fixes local data drop
Ignoring ownership rules
Thinking slice syntax is wrong
5. Given this function that returns a slice from two input slices, which lifetime annotation is correct to ensure safety?
fn longest_slice<'a>(x: &'a [i32], y: &'a [i32]) -> &____ [i32] {
if x.len() > y.len() { x } else { y }
}
hard
A. 'a + 'b
B. 'b
C. 'static
D. 'a
Solution
Step 1: Understand the function's return lifetime
The function returns a slice that could come from either input slice, both sharing lifetime 'a.
Step 2: Use shared lifetime for safety
The correct lifetime for the return is 'a, matching the lifetime of both input slices.
Final Answer:
'a -> Option D
Quick Check:
Return lifetime = 'a of inputs [OK]
Hint: Return lifetime must cover both inputs [OK]
Common Mistakes:
Choosing only one input lifetime
Using 'static without reason
Ignoring lifetime intersection rules
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.