A lifetime in Rust is a way to tell the compiler how long a reference should be valid. It helps prevent dangling references and ensures memory safety.
Click to reveal answer
beginner
Why do structs sometimes need lifetime annotations?
Structs need lifetime annotations when they hold references. This tells Rust how long those references are valid, so the compiler can check safety.
Click to reveal answer
beginner
How do you declare a struct with a lifetime parameter?
You add a lifetime parameter with an apostrophe before the lifetime name, like struct MyStruct<'a> { field: &'a str }. This means the field's reference lives at least as long as 'a.
Click to reveal answer
intermediate
What happens if you omit lifetime annotations on references in structs?
Rust will give a compile error because it can't be sure how long the references live. You must specify lifetimes to avoid dangling references.
Click to reveal answer
intermediate
Explain the difference between &'a T and T in a struct field.
&'a T is a reference with a lifetime 'a, meaning it borrows data that lives at least as long as 'a. T owns the data directly, so no lifetime is needed.
Click to reveal answer
Why do you add a lifetime parameter to a struct in Rust?
ATo allow the struct to own data
BTo make the struct mutable
CTo specify how long references inside the struct are valid
DTo enable multithreading
✗ Incorrect
Lifetime parameters tell Rust how long references inside the struct must be valid to ensure safety.
What does the syntax &'a str mean in a struct field?
AA reference to a string slice valid for lifetime 'a
BAn owned string with lifetime 'a
CA mutable string reference
DA static string
✗ Incorrect
It means a reference to a string slice that lives at least as long as the lifetime 'a.
What error occurs if you forget to add lifetime annotations to references in structs?
ACompile error about missing lifetime specifier
BRuntime error
CWarning only, code compiles
DNo error, Rust infers lifetimes automatically
✗ Incorrect
Rust requires explicit lifetime annotations on references in structs to ensure safety.
Which of these is a correct way to declare a struct with a lifetime?
Astruct Example { field: &i32 }
Bstruct Example<'a> { field: &'a i32 }
Cstruct Example<'a> { field: i32 }
Dstruct Example { field: &'a i32 }
✗ Incorrect
You must declare the lifetime parameter 'a on the struct and use it on the reference.
What does the lifetime parameter 'a guarantee?
AThe reference is mutable
BThe reference is static
CThe data is owned by the struct
DThe reference lives at least as long as 'a
✗ Incorrect
The lifetime 'a means the reference must be valid for at least the lifetime 'a.
Explain why lifetime annotations are necessary in structs that hold references.
Think about how Rust ensures memory safety with references inside structs.
You got /3 concepts.
Describe how to declare a struct with a lifetime parameter and a reference field.
Focus on the syntax for lifetime parameters and reference fields.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using lifetimes in Rust structs?
easy
A. To make structs mutable by default
B. To increase the speed of the program
C. To allow structs to own data permanently
D. To tell Rust how long references inside the struct are valid
Solution
Step 1: Understand what lifetimes represent
Lifetimes describe how long references are valid to prevent invalid memory access.
Step 2: Connect lifetimes to structs
In structs, lifetimes tell Rust how long the references inside the struct remain valid.
Final Answer:
To tell Rust how long references inside the struct are valid -> Option D
Quick Check:
Lifetimes = reference validity [OK]
Hint: Lifetimes track reference validity duration in structs [OK]
Common Mistakes:
Thinking lifetimes control program speed
Confusing ownership with lifetimes
Assuming lifetimes make structs mutable
2. Which of the following is the correct syntax to declare a struct with a lifetime parameter named 'a'?
easy
A. struct Example { data: &str<'a> }
B. struct Example<'a> { data: &'a str }
C. struct Example { data: &'a str }
D. struct Example<'a> { data: &str'a }
Solution
Step 1: Recall lifetime syntax in structs
Lifetime parameters are declared after the struct name with angle brackets, e.g., 'a.
Step 2: Match correct placement of lifetime
The lifetime must be declared as 'a after struct name and used before the reference type.