A variable lifetime is the period during which a variable is valid and can be used in the program. It starts when the variable is created and ends when it goes out of scope.
Click to reveal answer
beginner
Why does Rust use lifetimes?
Rust uses lifetimes to ensure references are always valid and to prevent bugs like dangling pointers by checking how long references live at compile time.
Click to reveal answer
beginner
What happens if a reference outlives the data it points to?
Rust will give a compile-time error because the reference would be invalid, preventing unsafe memory access.
Click to reveal answer
intermediate
How do you specify a lifetime in a function signature?
You use an apostrophe followed by a name, like &'a, to tell Rust that the reference has a certain lifetime named 'a.
Click to reveal answer
beginner
What is the scope of a variable in Rust?
The scope is the part of the code where the variable is accessible. When the scope ends, the variable is dropped and its lifetime ends.
Click to reveal answer
What does a variable lifetime represent in Rust?
AThe time a variable is valid and accessible
BThe size of the variable in memory
CThe number of times a variable is used
DThe type of the variable
✗ Incorrect
A variable lifetime is the period during which the variable is valid and can be accessed.
Why does Rust enforce lifetimes on references?
ATo allow variables to change type
BTo make the code run faster
CTo prevent invalid references and memory bugs
DTo increase the size of the program
✗ Incorrect
Rust uses lifetimes to ensure references do not outlive the data they point to, preventing bugs.
What symbol is used to name a lifetime in Rust?
A#
B&
C$
D'
✗ Incorrect
Lifetimes are named using an apostrophe, like 'a, in Rust.
What happens if a reference's lifetime is longer than the data it points to?
ARust allows it without warnings
BRust gives a compile-time error
CThe program crashes at runtime
DThe reference is automatically copied
✗ Incorrect
Rust prevents this by giving a compile-time error to avoid invalid memory access.
When does a variable's lifetime end in Rust?
AWhen the variable goes out of scope
BWhen the variable is reassigned
CWhen the program finishes
DWhen the variable is printed
✗ Incorrect
A variable's lifetime ends when it goes out of scope, meaning it is no longer accessible.
Explain what a variable lifetime is and why it matters in Rust.
Think about how long a variable can be used safely.
You got /3 concepts.
Describe how Rust uses lifetimes to prevent bugs with references.
Focus on how Rust checks references before running the program.
You got /3 concepts.
Practice
(1/5)
1. What does a variable lifetime in Rust describe?
easy
A. How long a reference remains valid in the program
B. The size of a variable in memory
C. The number of times a variable is used
D. The type of a variable
Solution
Step 1: Define lifetime and evaluate options
In Rust, lifetime refers to how long a reference is valid and safe to use. Only 'How long a reference remains valid in the program' correctly describes it; B is size, C is usage count, D is type.
Final Answer:
How long a reference remains valid in the program -> Option A
Quick Check:
Lifetime = reference validity duration [OK]
Hint: Lifetime means reference validity duration [OK]
Common Mistakes:
Confusing lifetime with variable size
Thinking lifetime counts usage times
Mixing lifetime with variable type
2. Which of the following is the correct syntax to declare a function with a lifetime parameter 'a for a reference argument in Rust?
easy
A. fn example(x: &i32) -> &i32 { x }
B. fn example(x: &i32<'a>) -> &i32<'a> { x }
C. fn example<'a>(x: &'a i32) -> &'a i32 { x }
D. fn example<'a>(x: &i32) -> &'a i32 { x }
Solution
Step 1: Verify lifetime annotation syntax and check options
Rust requires lifetime parameters declared with 'a before the function and used in references as &'a i32. A correctly declares and uses 'a for input/output. B misplaces after type, C lacks annotations, D misses on input.
Final Answer:
fn example<'a>(x: &'a i32) -> &'a i32 { x } -> Option C
Hint: Declare lifetime before function, use on references [OK]
Common Mistakes:
Placing lifetime after type instead of before
Omitting lifetime on input or output references
Using lifetime without declaring it
3. What will be the output of this Rust code snippet?
fn main() {
let mut r;
{
let x = 5;
r = &x;
}
println!("{}", r);
}
medium
A. 5
B. Compilation error due to lifetime mismatch
C. 0
D. Runtime panic
Solution
Step 1: Analyze scopes and reference lifetime
x goes out of scope at end of inner block, but r references it and lives longer, violating Rust's lifetime rules, resulting in compilation error.
Final Answer:
Compilation error due to lifetime mismatch -> Option B
Quick Check:
Reference lifetime must not outlive data [OK]
Hint: References can't outlive the data they point to [OK]
Common Mistakes:
Assuming code prints 5 without error
Expecting runtime panic instead of compile error
Ignoring inner block scope
4. Identify the error in this Rust function related to lifetimes:
fn longest(x: &str, y: &str) -> &str {
if x.len() > y.len() { x } else { y }
}
medium
A. Missing lifetime annotations on parameters and return type
B. Using &str instead of String
C. Function should return usize instead of &str
D. No error, code is correct
Solution
Step 1: Examine function signature and Rust lifetime rules
The function returns a reference to one of the inputs without lifetime annotations, preventing Rust from verifying safety. Annotations like <'a> are required on params and return.
Final Answer:
Missing lifetime annotations on parameters and return type -> Option A
Quick Check:
Return references need lifetime annotations [OK]
Hint: Add lifetime annotations when returning references [OK]
Common Mistakes:
Thinking &str vs String causes error
Believing function should return length
Assuming no error without lifetimes
5. Given this struct and function, which lifetime annotation is correct to ensure the reference inside Holder is valid?
A. fn create_holder(s: &'a str) -> Holder<'a> { Holder { value: s } }
B. fn create_holder(s: &str) -> Holder { Holder { value: s } }
C. fn create_holder<'a>(s: &str) -> Holder<'a> { Holder { value: s } }
D. fn create_holder<'a>(s: &'a str) -> Holder<'a> { Holder { value: s } }
Solution
Step 1: Match function signature to struct lifetime parameter
Holder<'a> ties value's lifetime to 'a. Function must declare 'a, use on s: &'a str, return Holder<'a>. D correct; A lacks fn 'a decl, B/C mismatch lifetimes.
Final Answer:
fn create_holder<'a>(s: &'a str) -> Holder<'a> { Holder { value: s } } -> Option D
Quick Check:
Function lifetimes must match struct lifetimes [OK]
Hint: Match function and struct lifetimes exactly [OK]
Common Mistakes:
Omitting lifetime on function signature
Using lifetime only on return or input, not both
Assuming struct lifetime is optional
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.