
Understand it
Box has exactly one owner and puts the value on the heap. Rc shares ownership on a single thread with a plain count. Arc does the same across threads with an atomic count, which costs more. Choosing the narrowest one that fits is the whole answer.
All three can place data behind a pointer, but they solve different ownership problems. Box<T> owns one heap allocation and is useful for recursive structures, large values, or a stable sized pointer to a trait object. Rc<T> lets several owners on one thread share one allocation through a non-atomic count. Arc<T> uses an atomic count for ownership shared across threads when the inner type permits it. Neither Rc nor Arc automatically grants mutable access to the shared value.
Use a concrete data flow: one parser builds a tree and hands it to one consumer, so Box may be enough. Several UI nodes on one thread need a shared immutable model, so Rc can be appropriate. Workers on different threads read the same configuration, so Arc fits. If they edit shared state, add a mutex, channel, or another synchronization design. Rc<RefCell<T>> provides runtime borrow checking on one thread, not cross-thread safety.
A strong answer includes costs and failure modes. Reference counting updates on clone and drop; Arc uses atomic operations, but that does not guarantee it is meaningfully slower in every application. Cycles of strong Rc or Arc links can leak; use Weak for back-references. Ask who should own the value and for how long before choosing a pointer. The simplest ownership model that expresses the required lifetime is usually easier to maintain.
Before reaching for Arc, ask if ownership can be transferred to one worker instead. Fewer shared owners simplify shutdown and error handling.
Interview follow-up: Draw a strong reference cycle and replace a back-reference with Weak. Why does memory safety allow a leak?
Read the code
struct Node { value: u32 }
fn main() {
let first = std::rc::Rc::new(Node { value: 7 });
let second = std::rc::Rc::clone(&first);
assert_eq!(second.value, 7);
}Rc shares ownership on one thread. Arc also shares ownership, but uses atomic counts and only crosses threads when its inner bounds permit it.
Try it
Match three scenarios to the narrowest pointer that satisfies them.
What success looks like: Every choice names an ownership count and a thread boundary.
Check your understanding
Which option expresses shared ownership on one thread without atomic reference counting?
Reveal the answer
- Box<Node> gives several independent owners of the same allocation.
- Rc<Node> matches this single-threaded shared-ownership requirement.
- Arc<Node> avoids all reference-count operations because its count is atomic.
Rc shares ownership on one thread. Arc also shares ownership, but uses atomic counts and only crosses threads when its inner bounds permit it.
Go deeper
Common misconception: Arc<Node> avoids all reference-count operations because its count is atomic.
Arc still updates its count on every clone and drop. Atomic means those updates are safe across threads, not that they disappear.
- Identify the contract: Three pointers, three different ownership questions.
- Trace the concrete example: Narrowest type that satisfies the requirement wins.
- Test the changed constraint: Draw a strong reference cycle and replace a back-reference with Weak. Why does memory safety allow a leak?
Transfer challenge: Draw a strong reference cycle and replace a back-reference with Weak. Why does memory safety allow a leak?
Connections
- Send and Sync: Thread-safety marker traits
- dyn Trait vs Generics: Dispatch and code size tradeoffs
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Box, Rc, and Arc (courseId "interview", lessonId "q-box-rc-arc") Link: https://crabrix.com/learn/interview/q-box-rc-arc/ Goal: Pick by ownership count first, then by thread boundary. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-box-rc-arc) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.