Smart pointers are data structures that act like a pointer but also have additional metadata and capabilities. In Rust, multiple smart pointers exists including reference counting smart pointer.

Smart pointers are usually implemented using structs that implements the Deref and Drop traits. The Deref trait allows an instance of the smart pointer struct to behave like a reference, and the Drop trait allows you to customize the code that’s run when an instance of the smart pointer goes out of scope.

In this chapter, 3 types of smart pointers in Rust are covered:

Box<T>: Point to Data on Heap

Boxes allow you to store data on the heap rather than the stack. They don’t have performance overhead and don’t have many extra capabilities either. It is used when:

Treating Smart Pointer Like Regular References with Deref Trait

Implementing the Deref trait allows you to customize the behavior of the deference operator *. Doing so, the smart pointer can be treated like a regular reference, and you can write code that operates on references and use that code with smart pointers too.

use std::ops::Deref;

impl<T> Deref for MyBox<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0 // fisrt value 
    }
}

Running Code on Cleanup with Drop Trait

Drop trait lets you customize what happens when a value is about to go out of scope. It requires you to implement one method named drop that takes a mutable reference to self.

If we want to explicitly call the drop on some variables that has the Drop trait, we could do std::mem::drop(var)

Rc<T>, the Reference Counted Smart Pointer

In a graph structure, multiple edges might point to the same node, and that node is conceptually owned by all of the edges that point to it. This node shouldn’t be cleaned up unless it doesn’t have any edges pointing to it.

Multiple ownership can be enabled by using Rc<T>, an abbreviation of reference counting. It keeps track of the number of references to a value to determine whether or not the value is still in use. If there’s no references to a value, the value can be cleaned up.