Rust Borrowing Quiz
45 in-depth questions covering Rust's borrowing system, immutable and mutable references, aliasing rules, reference scopes, and borrow checker validation — with 13 code examples to solidify understanding.
Question 1
What is borrowing in Rust?
Question 2
What is the syntax for creating an immutable reference?
Question 3
What is the syntax for creating a mutable reference?
Question 4
How many immutable references to the same value can exist simultaneously?
Question 5
Can you have a mutable reference and an immutable reference to the same value at the same time?
Question 6
What will this code do?
fn main() {
let mut x = 5;
let r1 = &x;
let r2 = &x;
println!("r1 = {}, r2 = {}", r1, r2);
}Question 7
What will this code do?
fn main() {
let mut x = 5;
let r1 = &mut x;
let r2 = &x;
println!("r1 = {}, r2 = {}", r1, r2);
}Question 8
What are the borrowing rules in Rust?
Question 9
What happens when you try to modify a value through an immutable reference?
Question 10
What is dereferencing in Rust?
Question 11
What will this code print?
fn main() {
let x = 5;
let r = &x;
println!("x = {}, r = {}", x, *r);
}Question 12
Can you dereference a mutable reference to modify the value?
Question 13
What is the scope of a reference?
Question 14
What will this code do?
fn main() {
let r;
{
let x = 5;
r = &x;
}
println!("{}", r);
}Question 15
What is the borrow checker?
Question 16
When does the borrow checker run?
Question 17
What is a dangling reference?
Question 18
How does Rust prevent dangling references?
Question 19
What will this code do?
fn main() {
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
s.push_str(" world");
println!("{} {}", r1, r2);
}Question 20
What will this code do?
fn main() {
let mut s = String::from("hello");
let r = &mut s;
r.push_str(" world");
println!("{}", r);
}Question 21
Can you have multiple mutable references to the same value?
Question 22
What is the difference between &T and &mut T?
Question 23
What will this code do?
fn main() {
let mut x = 5;
{
let y = &mut x;
*y = 10;
}
println!("{}", x);
}Question 24
What happens to references when they go out of scope?
Question 25
Can you return a reference from a function?
Question 26
What will this code do?
fn return_ref() -> &String {
let s = String::from("hello");
&s
}
fn main() {
let r = return_ref();
println!("{}", r);
}Question 27
What is implicit dereferencing?
Question 28
What will this code print?
fn main() {
let s = String::from("hello");
let r = &s;
println!("Length: {}", r.len());
}Question 29
What are the benefits of borrowing over ownership?
Question 30
When should you use borrowing instead of ownership?
Question 31
What will this code do?
fn main() {
let mut vec = vec![1, 2, 3];
let first = &vec[0];
vec.push(4);
println!("{}", first);
}Question 32
What is reference aliasing?
Question 33
Why does Rust restrict aliasing with mutation?
Question 34
What is the NLL (Non-Lexical Lifetimes) feature?
Question 35
What will this code do with NLL?
fn main() {
let mut x = 5;
let r = &x;
println!("{}", r);
let y = &mut x;
*y = 10;
println!("{}", y);
}Question 36
What is a borrow split?
Question 37
What will this code do?
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut p = Point { x: 1, y: 2 };
let rx = &mut p.x;
let ry = &mut p.y;
*rx = 10;
*ry = 20;
println!("{} {}", p.x, p.y);
}Question 38
What is the purpose of the RefCell<T> type?
Question 39
When should you use RefCell<T>?
Question 40
What is the difference between & and &mut borrows?
Question 41
What will this code do?
fn main() {
let s = String::from("hello");
let len = calculate_length(&s);
println!("Length of '{}' is {}", s, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}Question 42
Why is borrowing important for function parameters?
Question 43
What is the relationship between borrowing and lifetimes?
Question 44
What is the main advantage of Rust's borrowing system over garbage collection?
Question 45
When implementing a library API, how should you design borrowing in your function signatures?
