Rust Lifetimes Quiz
40 in-depth questions covering Rust's lifetime system, lifetime annotations, elision rules, function signatures with lifetimes, struct references, and common lifetime errors — with 11 code examples to solidify understanding.
Question 1
What are lifetimes in Rust?
Question 2
What is the syntax for lifetime annotations?
Question 3
When does Rust require explicit lifetime annotations?
Question 4
What are the lifetime elision rules?
Question 5
What is the first lifetime elision rule?
Question 6
What is the second lifetime elision rule?
Question 7
What is the third lifetime elision rule?
Question 8
What will this function signature elide to?
fn first_word(s: &str) -> &strQuestion 9
What will this function signature elide to?
fn longest(x: &str, y: &str) -> &strQuestion 10
What will this method signature elide to?
fn get_name(&self) -> &strQuestion 11
What is a lifetime parameter in a function?
Question 12
What does this function signature mean?
fn longest<'a>(x: &'a str, y: &'a str) -> &'a strQuestion 13
What will this code do?
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("short");
let result;
{
let s2 = String::from("longer");
result = longest(&s1, &s2);
}
println!("{}", result);
}Question 14
How can you fix the lifetime error in the previous question?
Question 15
What is a struct with lifetime parameters?
Question 16
What is the correct syntax for a struct with lifetimes?
Question 17
What will this code do?
struct Book<'a> {
title: &'a str,
}
fn main() {
let book;
{
let title = String::from("1984");
book = Book { title: &title };
}
println!("{}", book.title);
}Question 18
What is a common lifetime error message?
Question 19
What does 'borrowed value does not live long enough' mean?
Question 20
What is the 'static lifetime?
Question 21
When can you use 'static lifetime?
Question 22
What will this code do?
fn get_static() -> &'static str {
"hello"
}
fn main() {
let s = get_static();
println!("{}", s);
}Question 23
What is lifetime subtyping?
Question 24
What does this syntax mean?
fn foo<'a, 'b: 'a>(x: &'a str, y: &'b str) -> &'a strQuestion 25
What is the T: 'a syntax?
Question 26
What will this code do?
fn longest<'a>(x: &'a str, y: &str) -> &'a str {
x
}
fn main() {
let s1 = String::from("short");
let result;
{
let s2 = String::from("long");
result = longest(&s1, &s2);
}
println!("{}", result);
}Question 27
What is the difference between lifetime annotations and generic type parameters?
Question 28
What is a lifetime in a trait bound?
Question 29
What will this code do?
struct Ref<'a, T> {
value: &'a T,
}
fn main() {
let x = 5;
let r = Ref { value: &x };
println!("{}", r.value);
}Question 30
What is the HRTB (Higher-Ranked Trait Bounds) syntax?
Question 31
When would you use HRTB?
Question 32
What is a common source of lifetime errors in Rust?
Question 33
How can you fix 'does not live long enough' errors?
Question 34
What is lifetime variance?
Question 35
What is covariance in lifetimes?
Question 36
What is contravariance in lifetimes?
Question 37
What does invariant mean for lifetimes?
Question 38
When are mutable references invariant?
Question 39
What is the 'outlives' relationship?
Question 40
What is the most challenging aspect of learning lifetimes?
