Smart pointers and memory

Ownership alone cannot express every structure, so the standard library adds a few pointers with a rule attached. These six lessons put a value on the heap with Box, share it with Rc and mutate it through RefCell, then cross a thread with Arc and break a cycle with Weak. A guard written with Drop cleans up on the way out, and the last two lessons go under the surface: a buffer allocated and grown in unsafe code, and the global allocator that Vec asks for every block it takes.

073 Box: values on the heap Box::new, a recursive type that needs it, deref, and a trait object in a box (C++ 091) Open 074 Rc, RefCell and shared mutation reference counting, clone that shares, RefCell and its run-time borrow check, a tree that points back (C++ 092) Open 075 Arc, Weak and reference cycles Arc across threads, Weak that does not own, a cycle without a leak and upgrade (C++ 092) Open 076 Drop guards and custom cleanup Drop for a wrapper, a guard that closes a handle, and a cleanup that runs on the way out (C++ 093) Open 077 Allocation and a growable buffer alloc and dealloc in unsafe, a buffer that grows, and the same job done safely by Vec (C++ 089, 090) Open 078 The global allocator the GlobalAlloc trait, a counting allocator, and what Vec asks the allocator for (C++ 096) Open

Lessons