Rust Ownership Quiz
45 in-depth questions covering Rust's ownership system, move semantics, copy types, resource cleanup through Drop trait, precise drop timing, and ownership transfer patterns — with 12 code examples to solidify understanding.
Question 1
What are the three rules of ownership in Rust?
Question 2
What happens when you assign a String to another variable?
Question 3
Which of these types implements Copy in Rust?
Question 4
What is the purpose of the Drop trait?
Question 5
When is a value's drop method called?
Question 6
What happens to ownership when you pass a value to a function?
Question 7
How can you avoid moving ownership when passing to a function?
Question 8
What will this code do?
fn main() {
let s = String::from("hello");
take_ownership(s);
println!("{}", s); // This line
}
fn take_ownership(s: String) {
println!("{}", s);
}Question 9
What will this code do?
fn main() {
let s = String::from("hello");
borrow_string(&s);
println!("{}", s); // This line
}
fn borrow_string(s: &String) {
println!("{}", s);
}Question 10
When does Rust copy values instead of moving them?
Question 11
What is the difference between Copy and Clone?
Question 12
Can you implement Copy for a struct containing a String?
Question 13
What happens when a value with a custom Drop implementation goes out of scope?
Question 14
In what order are drop methods called for nested scopes?
Question 15
What will this code print?
struct CustomDrop {
id: i32,
}
impl Drop for CustomDrop {
fn drop(&mut self) {
println!("Dropping {}", self.id);
}
}
fn main() {
let a = CustomDrop { id: 1 };
{
let b = CustomDrop { id: 2 };
let c = CustomDrop { id: 3 };
}
println!("End of main");
}Question 16
How can you transfer ownership back from a function?
Question 17
What is ownership transfer useful for?
Question 18
When working with a database connection that needs to be closed when done, which ownership pattern is most appropriate?
Question 19
What happens when you move a value into a Vec?
Question 20
How can you get ownership back from a Vec?
Question 21
What will this code do?
fn main() {
let mut vec = Vec::new();
let s = String::from("hello");
vec.push(s);
println!("{}", s); // This line
}Question 22
When implementing a cache that stores expensive-to-create objects, which ownership pattern should you use?
Question 23
What is the relationship between ownership and borrowing?
Question 24
When should you prefer moving over borrowing?
Question 25
What happens when you move a value that implements Drop?
Question 26
In a web server handling requests, how should you manage database connections?
Question 27
What is the key difference between moving and copying?
Question 28
When implementing a file reader that should close the file automatically, which pattern should you use?
Question 29
What will this code print?
fn main() {
let x = 5;
let y = x;
println!("x = {}, y = {}", x, y);
}Question 30
What will this code do?
fn main() {
let s = String::from("hello");
let t = s;
println!("s = {}", s);
}Question 31
When should you implement Copy for your own types?
Question 32
What happens if you try to derive Copy for a struct with a Vec field?
Question 33
In a system managing temporary files, how should you handle file cleanup?
Question 34
What is the performance implication of moving large structs?
Question 35
When implementing a parser that builds an AST, how should you handle node ownership?
Question 36
What will this code print?
struct NoisyDrop {
id: i32,
}
impl Drop for NoisyDrop {
fn drop(&mut self) {
println!("Dropping {}", self.id);
}
}
fn main() {
let a = NoisyDrop { id: 1 };
let b = a;
println!("End of main");
}Question 37
When should you avoid implementing Drop?
Question 38
In a multi-threaded application, how does ownership help prevent data races?
Question 39
What happens when you return a large struct from a function?
Question 40
When implementing a smart pointer like Box<T>, how should you handle the inner value's ownership?
Question 41
What is the relationship between ownership and the borrow checker?
Question 42
In a library providing an API, how should you design function signatures for ownership?
Question 43
What will this code do?
fn main() {
let mut vec = Vec::new();
for i in 0..3 {
let s = format!("item {}", i);
vec.push(s);
}
println!("Vec has {} items", vec.len());
// vec goes out of scope here
}Question 44
When implementing a configuration struct that holds database URLs, which ownership pattern is most appropriate?
Question 45
What is the most important benefit of Rust's ownership system?
