Smart pointers and sharing
Box puts one value on the heap. Rc and Arc share it, with a count. Cow borrows until it
must copy. Those four sentences cover most of the chapter.
The rest is what follows: what Deref does to method calls, when a cycle leaks, and why the process
memory keeps growing.
Articles
Section titled “Articles”Nothing published in this chapter yet — all 18 articles are still to come.
Coming up (18)
- What is a smart pointer?a struct that owns and derefs
- What is Box, and when should you use it?one allocation, one owner
- How to create a recursive type with Boxa linked list without infinite size
- What is Rc, and how does it differ from Box?reference counting in one thread
- What is Arc, and when should you use it?Rc with an atomic count
- What is the difference between Rc and Arc?and why Rc is not Send
- What is the difference between Box, Rc, and Arc?a table, and how to pick
- How to fix “cannot move out of an Rc”try_unwrap, clone, or restructure
- How to avoid reference cycles with Weakthe leak a cycle makes
- How does Deref make a smart pointer act like a reference?method resolution, step by step
- How does Drop work for smart pointers?the count reaching zero
- What is Cow, and when should you use it?borrow until you must own
- When does Rust allocate on the heap?the four things that allocate
- Can a Rust program leak memory?safely leaking, and Rc cycles
- How to fix a stack overflow from deep recursionthe default stack is smaller than you think
- Why is my Rust process’s memory growing?leaks, caches, and allocators that do not return
- How to check the size of a Rust typesize_of, alignment, and the padding you did not expect
- How to avoid padding in a structfield order changes the size