Rust Smart Pointers Quiz
Master Rust's smart pointer types for automatic memory management and safe heap allocation
Question 1
What is a smart pointer in Rust?
Question 2
What does Box<T> do?
Question 3
What does this Box example do?
let b = Box::new(5);
println!("b = {}", b);Question 4
When would you use Box<T>?
Question 5
What is Rc<T>?
Question 6
What does this Rc example show?
use std::rc::Rc;
let a = Rc::new(vec![1, 2, 3]);
let b = Rc::clone(&a);
println!("Reference count: {}", Rc::strong_count(&a));Question 7
What is the difference between Rc::clone() and regular clone()?
Question 8
What is Arc<T>?
Question 9
When would you choose Arc<T> over Rc<T>?
Question 10
What is RefCell<T>?
Question 11
What does this RefCell example do?
use std::cell::RefCell;
let c = RefCell::new(5);
*c.borrow_mut() = 10;
println!("Value: {}", *c.borrow());Question 12
What happens if you violate borrowing rules with RefCell?
Question 13
What is Weak<T>?
Question 14
How do you create a Weak<T> from Rc<T>?
Question 15
What does this Weak example show?
use std::rc::{Rc, Weak};
let strong = Rc::new(5);
let weak = Rc::downgrade(&strong);
if let Some(value) = weak.upgrade() {
println!("Value: {}", value);
}Question 16
What is a reference cycle?
Question 17
How do you break reference cycles?
Question 18
What does Deref trait provide?
Question 19
What does Drop trait do?
Question 20
What does this custom Drop example do?
struct CustomSmartPointer {
data: String,
}
impl Drop for CustomSmartPointer {
fn drop(&mut self) {
println!("Dropping with data: {}", self.data);
}
}Question 21
What is the performance cost of Rc<T>?
Question 22
What is the performance cost of Arc<T>?
Question 23
What is the performance cost of RefCell<T>?
Question 24
What does Rc<RefCell<T>> allow?
Question 25
What does Arc<Mutex<T>> allow?
Question 26
What is interior mutability?
Question 27
What is the difference between Cell<T> and RefCell<T>?
Question 28
What does this Cell example show?
use std::cell::Cell;
let c = Cell::new(5);
c.set(10);
println!("Value: {}", c.get());Question 29
What is a common use case for Box<T>?
Question 30
What is a common use case for Rc<T>?
Question 31
What is a common use case for Arc<T>?
Question 32
What is a common use case for RefCell<T>?
Question 33
What is a common use case for Weak<T>?
Question 34
What happens when Rc strong count reaches zero?
Question 35
What happens when Arc strong count reaches zero?
Question 36
Can you implement Deref for your own types?
Question 37
What is Deref coercion?
Question 38
What does this Deref coercion example show?
fn takes_str(s: &str) { println!("{}", s); }
let s = Box::new(String::from("hello"));
takes_str(&s);Question 39
What is the memory layout of Box<T>?
Question 40
In a complex application with a tree structure where nodes need to reference their parent, shared configuration across threads, and mutable cached data, which smart pointer combination would you choose and why?
