Rust Memory Management Quiz
Deep dive into Rust's ownership system, borrowing rules, and advanced memory management patterns for safe and efficient code
Question 1
What are the three ownership rules in Rust?
Question 2
What does this ownership example show?
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);Question 3
What is the difference between stack and heap allocation?
Question 4
What does Copy trait allow?
Question 5
What does Clone trait allow?
Question 6
What are borrowing rules?
Question 7
What does this borrowing example show?
let mut s = String::from("hello");
let r1 = &s;
let r2 = &s;
let r3 = &mut s;Question 8
What is a lifetime?
Question 9
What does this lifetime example show?
fn longest<'a>(s1: &'a str, s2: &'a str) -> &'a str {
if s1.len() > s2.len() { s1 } else { s2 }
}Question 10
What is lifetime elision?
Question 11
What is a dangling reference?
Question 12
What does this dangling reference example show?
fn dangle() -> &String {
let s = String::from("hello");
&s
}Question 13
What is RAII?
Question 14
What does Drop trait do?
Question 15
What does this custom Drop example show?
struct CustomDrop {
data: String,
}
impl Drop for CustomDrop {
fn drop(&mut self) {
println!("Dropping: {}", self.data);
}
}Question 16
What is the difference between & and &mut?
Question 17
What is interior mutability?
Question 18
What is the difference between Cell<T> and RefCell<T>?
Question 19
What is a raw pointer?
Question 20
What does unsafe keyword allow?
Question 21
What is memory layout in Rust?
Question 22
What does #[repr(C)] do?
Question 23
What is ownership transfer (move)?
Question 24
What is partial move?
Question 25
What does this partial move example show?
struct Person {
name: String,
age: u32,
}
let p = Person { name: String::from("Alice"), age: 30 };
let name = p.name;
// p.age is still accessible, p.name is movedQuestion 26
What is the difference between String and &str?
Question 27
What is the difference between Vec<T> and &[T]?
Question 28
What is deref coercion?
Question 29
What does this deref coercion example show?
fn takes_str(s: &str) { println!("{}", s); }
let s = String::from("hello");
takes_str(&s);Question 30
What is the difference between Sized and ?Sized?
Question 31
What is DST (Dynamically Sized Type)?
Question 32
What is memory alignment?
Question 33
What is padding in structs?
Question 34
What is the difference between fat and thin pointers?
Question 35
What is stack allocation vs heap allocation performance?
Question 36
What is the borrow checker?
Question 37
What is NLL (Non-Lexical Lifetimes)?
Question 38
What is the difference between lifetime bounds and trait bounds?
Question 39
What is memory safety?
Question 40
In a complex data structure with trees of objects that reference each other, shared configuration data accessed by multiple threads, and temporary buffers with varying lifetimes, which memory management patterns would you choose and why?
