0
0
Rustprogramming~5 mins

Ownership with smart pointers in Rust - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a smart pointer in Rust?
A smart pointer is a data structure that not only holds a value but also has extra metadata and capabilities, like managing memory automatically.
Click to reveal answer
beginner
How does Box<T> help with ownership?
Box<T> stores data on the heap and gives ownership to a single owner, automatically freeing memory when it goes out of scope.
Click to reveal answer
intermediate
What problem does Rc<T> solve?
Rc<T> allows multiple parts of code to share ownership of the same data by keeping a count of references, freeing memory only when the last owner is gone.
Click to reveal answer
intermediate
Why use RefCell<T> with Rc<T>?
RefCell<T> allows mutable borrowing checked at runtime, so you can change data inside an Rc<T> even though Rc<T> itself is immutable.
Click to reveal answer
advanced
What happens if you try to create a reference cycle with Rc<T>?
A reference cycle causes a memory leak because the reference count never reaches zero, so Rust can’t free the memory automatically.
Click to reveal answer
Which smart pointer in Rust allows multiple owners of the same data?
ARc<T>
BBox<T>
CRefCell<T>
DString
What does Box<T> do with the data it owns?
AStores it on the stack
BStores it on the heap
CCopies it automatically
DShares ownership
Which smart pointer allows mutation checked at runtime inside an immutable owner?
AVec<T>
BBox<T>
CRc<T>
DRefCell<T>
What is a risk of using Rc<T> without care?
AStack overflow
BData races
CReference cycles causing memory leaks
DCompile-time errors
When does Rust free the memory owned by a Box<T>?
AWhen the Box goes out of scope
BWhen the Box is cloned
CWhen the program ends
DWhen the data is copied
Explain how ownership works with Box<T> and why it is useful.
Think about how Box stores data and what happens when it goes out of scope.
You got /4 concepts.
    Describe the difference between Rc<T> and RefCell<T> and when you might use them together.
    One manages how many owners exist, the other allows changing data inside an immutable owner.
    You got /4 concepts.