Sample questions
Question 1Easy
What is the type of a string literal such as "hello"?
- String (the owned heap type)
- str (an unsized slice, used without a reference)
- &'static str
- &mut String (a mutable borrow of a String)
Answer: C. &'static str
Explanation
Literals are stored in the program binary and live for the whole run, so they are &'static str.
Step-by-step solution
- Literals sit in read-only memory.
- They live for the entire program.
- Type is &'static str.
Why the other options are wrong
- A. String is the owned heap type.
- B. str alone is unsized; you use it behind a reference.
- D. Literals are immutable and not String.
Question 2Easy
Which creates a mutable borrow of x?
- mut &x, which marks the shared reference as writable
- *x
- &x
- &mut x (x must be declared mut)
Answer: D. &mut x (x must be declared mut)
Explanation
A mutable reference requires the original binding to be mutable.
Step-by-step solution
- &x is shared borrow.
- &mut x is a mutable borrow.
- x must be declared mut.
Why the other options are wrong
- A. mut &x is not a mutable borrow.
- B. *x dereferences.
- C. &x is immutable.
Question 3Easy
How many mutable references to the same value can be alive in the same scope at one time?
- Two
- One
- Any number
- None; mutable references are not allowed
Answer: B. One
Explanation
Rust allows exactly one &mut at a time (and no shared refs alongside it). This prevents data races and iterator invalidation.
Step-by-step solution
- Rule: one writer or many readers.
- Two
&mut conflict. - So the answer is one.
Why the other options are wrong
- A. Two would allow aliasing while mutating.
- C. Unlimited mutable access would allow data races.
- D.
&mut is allowed, just exclusively.
Question 4Easy
What does &x create in Rust?
- A shared (immutable) reference that borrows x
- A copy of x
- A mutable reference that allows changing x through the borrow
- A raw pointer
Answer: A. A shared (immutable) reference that borrows x
Explanation
Borrowing gives access without taking ownership; &mut x creates a mutable reference.
Step-by-step solution
- The & operator creates a reference.
- A plain &x is shared and immutable, so it only reads x.
- So option A is the correct answer.
Why the other options are wrong
- B. No copy is made; the reference borrows x.
- C. A mutable reference is written &mut x.
- D. A raw pointer has different types and is an unsafe concept.
Tip: &x borrows for reading; &mut x borrows for changing.
Question 5Medium
What is a dangling reference, and how does Rust handle it?
- A reference that is never used; it is a warning only
- A reference to freed data; Rust rejects it at compile time
- A null pointer; Rust allows it in safe code
- A reference to a global; Rust forbids them entirely in safe code
Answer: B. A reference to freed data; Rust rejects it at compile time
Explanation
The compiler ensures data outlives every reference to it.
Step-by-step solution
- A dangling reference points at data that has been freed.
- Rust's borrow checker ensures data outlives every reference to it.
- So such code is rejected at compile time; option B is the correct answer.
Why the other options are wrong
- A. A dangling reference is an error, not a warning.
- C. Safe Rust references are never null.
- D. References to global data are allowed in safe code, for example to 'static data.
Tip: Rust rejects dangling references at compile time, so no runtime check is needed.
Question 6Medium
What does the 'static lifetime mean?
- The data can live for the entire program, e.g. string literals
- The data is on the stack
- The variable is mutable
- The reference is freed at the end of the current function, like a local
Answer: A. The data can live for the entire program, e.g. string literals
Explanation
&'static str literals are embedded in the binary; T: 'static means T holds no short-lived references.
Step-by-step solution
- 'static means the data can live for the whole program.
- String literals are stored in the binary and have the 'static lifetime.
- So option A is the correct answer.
Why the other options are wrong
- B. 'static is about lifetime, not about being on the stack.
- C. Mutability is set with mut, not by 'static.
- D. This describes a local lifetime, not 'static.
Tip: 'static means the data can last for the whole run of the program; it is not a mutable global.
Question 7Medium
What are Non-Lexical Lifetimes (NLL)?
- A feature that lets references outlive the data they point to when it is safe
- References that live forever
- Lifetimes that cannot be named
- Borrows end at their last use, not the end of the scope
Answer: D. Borrows end at their last use, not the end of the scope
Explanation
The borrow checker reasons about where references are actually used, accepting more correct programs.
Step-by-step solution
- Before NLL, a borrow was treated as lasting until the end of its scope.
- NLL ends a borrow at its last use instead.
- So option D is the correct answer.
Why the other options are wrong
- A. NLL does not let references outlive their data; it only shortens borrows.
- B. References do not live forever because of NLL.
- C. Lifetimes can still be named, for example with 'a.
Tip: NLL lets more correct programs compile without extra annotations.
Question 8Medium
Why does this fail to compile?
let mut v = vec![1, 2, 3];
let a = &mut v;
let b = &mut v;
a.push(4);
- Two mutable borrows of v are active at the same time
- The vector is moved into a, and b tries to use the moved value
- push needs a shared reference
- vec! cannot be mutable
Answer: A. Two mutable borrows of v are active at the same time
Explanation
a is still used after b is created, so both &mut borrows overlap.
Step-by-step solution
- Let a = &mut v; and let b = &mut v; create two mutable borrows of v.
- a.push(4) uses a after b was created, so the two borrows overlap.
- Two active mutable borrows break the rule, so option A is the correct answer.
Why the other options are wrong
- B. &mut v borrows v; it does not move v into a.
- C. Push needs a mutable reference, not a shared one.
- D. Vec! can be declared mut, as the code does with let mut v.
Tip: Keep a mutable borrow short, or end it before creating another one.
Question 9Hard
Why does a function returning a reference often need a lifetime annotation like fn longest<'a>(x: &'a str, y: &'a str) -> &'a str?
- To allocate the result on the heap
- Because every function with references must list lifetimes, even with one input
- To make the function faster
- To tell the compiler how the output's lifetime relates to the inputs
Answer: D. To tell the compiler how the output's lifetime relates to the inputs
Explanation
With two reference inputs, the compiler cannot infer which one the result borrows from; elision rules cover simpler cases.
Step-by-step solution
- With two reference inputs, the compiler cannot tell which one the output borrows from.
- A lifetime annotation states how the output's lifetime relates to the inputs.
- So option D is the correct answer.
Why the other options are wrong
- A. Lifetimes do not decide where a result is allocated.
- B. Elision rules cover simple cases, so annotations are not needed for every function.
- C. Lifetime annotations do not change speed.
Tip: Annotate lifetimes when the output could borrow from more than one input.
Question 10Hard
A struct holding a reference, like struct Parser<'a> { input: &'a str }, means:
- A Parser cannot outlive the string it borrows
- The string is copied into the struct
- The struct must be stored in a Box so the reference stays valid on the heap
- The struct owns the string
Answer: A. A Parser cannot outlive the string it borrows
Explanation
The lifetime parameter ties the struct's validity to the borrowed data.
Step-by-step solution
- The lifetime 'a in Parser<'a> ties the struct to the borrowed string.
- The struct holds only a reference, so it cannot outlive that string.
- So option A is the correct answer.
Why the other options are wrong
- B. The string is borrowed, not copied into the struct.
- C. A Box is not needed for the reference to stay valid.
- D. The struct does not own the string; it borrows it.
Tip: A struct with a lifetime parameter is a borrowed view; its data must live at least as long.
Practise all 18 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →