Rust Lifetimes Quiz

Rust
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

What are lifetimes in Rust?

A
How long a program runs
B
The scope for which a reference is valid
C
Memory allocation duration
D
Variable declaration time
2

Question 2

What is the syntax for lifetime annotations?

A
'a
B
&a
C
a'
D
'a'
3

Question 3

When does Rust require explicit lifetime annotations?

A
Always
B
Never
C
When the compiler cannot infer lifetimes automatically
D
Only for structs
4

Question 4

What are the lifetime elision rules?

A
Rules that allow omitting lifetime annotations in function signatures
B
Rules for extending variable lifetimes
C
Rules for garbage collection
D
Rules for memory allocation
5

Question 5

What is the first lifetime elision rule?

A
Each parameter gets its own lifetime
B
All parameters get the same lifetime
C
Return value gets a new lifetime
D
No elision for return values
6

Question 6

What is the second lifetime elision rule?

A
If there's exactly one input lifetime, it's assigned to all output lifetimes
B
Each output gets its own lifetime
C
Outputs cannot have lifetimes
D
Only one output lifetime allowed
7

Question 7

What is the third lifetime elision rule?

A
Methods get &self lifetimes
B
If there are multiple input lifetimes, one is &self, outputs get &self lifetime
C
Methods cannot elide lifetimes
D
Methods always need explicit lifetimes
8

Question 8

What will this function signature elide to?

rust
fn first_word(s: &str) -> &str
A
fn first_word<'a>(s: &'a str) -> &'a str
B
fn first_word<'a>(s: &str) -> &'a str
C
fn first_word(s: &str) -> &str
D
Compilation error
9

Question 9

What will this function signature elide to?

rust
fn longest(x: &str, y: &str) -> &str
A
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
B
fn longest<'a, 'b>(x: &'a str, y: &'b str) -> &'a str
C
Compilation error
D
No elision possible
10

Question 10

What will this method signature elide to?

rust
fn get_name(&self) -> &str
A
fn get_name<'a>(&'a self) -> &'a str
B
fn get_name(&self) -> &str
C
Compilation error
D
fn get_name<'a>(&self) -> &'a str
11

Question 11

What is a lifetime parameter in a function?

A
A parameter that specifies how long the function runs
B
A generic parameter that constrains reference lifetimes
C
A parameter for memory allocation
D
A parameter for function timeout
12

Question 12

What does this function signature mean?

rust
fn longest<'a>(x: &'a str, y: &'a str) -> &'a str
A
x and y have different lifetimes
B
x and y have the same lifetime, return has that lifetime
C
The return has a different lifetime
D
No lifetime constraints
13

Question 13

What will this code do?

rust
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);
}
A
Print 'longer'
B
Compilation error
C
Print 'short'
D
Runtime panic
14

Question 14

How can you fix the lifetime error in the previous question?

A
Use different lifetime parameters
B
Move the println! inside the block
C
Use Rc<T>
D
Remove the lifetime annotation
15

Question 15

What is a struct with lifetime parameters?

A
A struct that lives for a specific time
B
A struct containing references with lifetime constraints
C
A struct with timing methods
D
A struct for measuring performance
16

Question 16

What is the correct syntax for a struct with lifetimes?

A
struct Foo<'a> { field: &'a str }
B
struct Foo { field: &'a str }
C
struct Foo<'a> { field: &str }
D
struct Foo { field: &str }
17

Question 17

What will this code do?

rust
struct Book<'a> {
    title: &'a str,
}

fn main() {
    let book;
    {
        let title = String::from("1984");
        book = Book { title: &title };
    }
    println!("{}", book.title);
}
A
Print '1984'
B
Compilation error
C
Runtime panic
D
Print garbage
18

Question 18

What is a common lifetime error message?

A
'a does not live long enough
B
Expected &str, found String
C
Cannot move out of borrowed content
D
Mismatched types
19

Question 19

What does 'borrowed value does not live long enough' mean?

A
The value is deallocated too early
B
The value lives too long
C
The value is copied
D
The value is moved
20

Question 20

What is the 'static lifetime?

A
The lifetime of the program
B
A very short lifetime
C
The lifetime of a function
D
The lifetime of a loop
21

Question 21

When can you use 'static lifetime?

A
For all references
B
For string literals and static variables
C
For local variables
D
Never
22

Question 22

What will this code do?

rust
fn get_static() -> &'static str {
    "hello"
}

fn main() {
    let s = get_static();
    println!("{}", s);
}
A
Print 'hello'
B
Compilation error
C
Runtime panic
D
Print nothing
23

Question 23

What is lifetime subtyping?

A
One lifetime being shorter than another
B
Lifetimes being equal
C
Lifetimes being longer
D
Lifetime conversion
24

Question 24

What does this syntax mean?

rust
fn foo<'a, 'b: 'a>(x: &'a str, y: &'b str) -> &'a str
A
'a is shorter than 'b
B
'b outlives 'a
C
'a and 'b are equal
D
'a outlives 'b
25

Question 25

What is the T: 'a syntax?

A
T lives longer than 'a
B
Any references in T must outlive 'a
C
T has lifetime 'a
D
T is borrowed for 'a
26

Question 26

What will this code do?

rust
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);
}
A
Print 'short'
B
Compilation error
C
Runtime panic
D
Print 'long'
27

Question 27

What is the difference between lifetime annotations and generic type parameters?

A
No difference
B
Lifetimes constrain references, generics constrain types
C
Lifetimes are for types, generics for references
D
Lifetimes replace generics
28

Question 28

What is a lifetime in a trait bound?

A
How long the trait is valid
B
Constraining associated types or references in traits
C
Trait method duration
D
Trait implementation time
29

Question 29

What will this code do?

rust
struct Ref<'a, T> {
    value: &'a T,
}

fn main() {
    let x = 5;
    let r = Ref { value: &x };
    println!("{}", r.value);
}
A
Print 5
B
Compilation error
C
Runtime panic
D
Print 0
30

Question 30

What is the HRTB (Higher-Ranked Trait Bounds) syntax?

A
for<'a> syntax for higher-ranked lifetimes
B
A way to specify longer lifetimes
C
A replacement for lifetime annotations
D
A way to avoid lifetimes
31

Question 31

When would you use HRTB?

A
For closures that return references
B
For simple function signatures
C
For struct definitions
D
Never
32

Question 32

What is a common source of lifetime errors in Rust?

A
Using the wrong variable names
B
Trying to return references to local variables
C
Using the wrong data types
D
Incorrect indentation
33

Question 33

How can you fix 'does not live long enough' errors?

A
Always use 'static
B
Restructure code to match lifetimes or return owned values
C
Use unsafe code
D
Ignore the error
34

Question 34

What is lifetime variance?

A
How lifetimes change in subtyping
B
Lifetime performance
C
Lifetime syntax
D
Lifetime conversion
35

Question 35

What is covariance in lifetimes?

A
Shorter lifetimes can be used as longer ones
B
Longer lifetimes can be used as shorter ones
C
Lifetimes cannot be converted
D
All lifetimes are equal
36

Question 36

What is contravariance in lifetimes?

A
Function that takes &'long T can accept &'short T
B
Function that takes &'short T can accept &'long T
C
Lifetimes are invariant
D
No contravariance in Rust
37

Question 37

What does invariant mean for lifetimes?

A
Lifetimes can be converted freely
B
Lifetimes cannot be converted
C
Lifetimes are covariant
D
Lifetimes are contravariant
38

Question 38

When are mutable references invariant?

A
Always
B
For safety reasons
C
For performance
D
Never
39

Question 39

What is the 'outlives' relationship?

A
'a: 'b means 'a lives longer than 'b
B
'a: 'b means 'b lives longer than 'a
C
'a: 'b means they are equal
D
'a: 'b is invalid syntax
40

Question 40

What is the most challenging aspect of learning lifetimes?

A
The syntax
B
Thinking about reference validity and relationships
C
Performance implications
D
Integration with borrowing

QUIZZES IN Rust