Start with the idea
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.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.