Types of smart pointers

Some of the smart pointer types from the standard library as follows:

  • Box<T>: This provides the simplest form of heap allocation. The Box type owns the value inside it, and can thus be used for holding values inside structs or for returning them from functions.
  • Rc<T>: This is for reference counting. It increments a counter whenever somebody takes a new reference, and decrements it when someone releases a reference. When the counter hits zero, the value is dropped.
  • Arc<T>: This is for atomic reference counting. This is like the previous type, but with atomicity to guarantee multithread safety.
  • 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. It uses runtime locking for safety.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset