O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Collector types

Next, we'll take a look at a few generic types by which we can control how the memory allocation in the heap is done. The types are as follows:

  • Box<T>: This is the simplest form of heap allocation. The box owns the value inside it, and can thus be used for holding values inside structs or for returning them from functions.
  • Cell<T>: This gives us internal mutability for types that implement the Copy trait. In other words, we gain the possibility to get multiple mutable references to something.
  • RefCell<T>: This gives us internal mutability for types, without requiring the Copy trait. Uses runtime locking for safety.
  • Rc<T>: This is for reference counting. It increments a counter whenever somebody takes a new reference, decrements ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required