Smart Pointers

Explore the Box smart pointer, a valuable tool for dynamic memory allocation and ownership control.

Conceptually, in Rust, pointers are memory addresses stored on the stack that refer to, or "point at", a memory location in the heap. The kind of pointers that we've used till now are references, denoted by the ampersand (&) symbol. They merely point to the address holding the data and do not have any overhead.

On the other hand, smart pointers are pointers but with additional capabilities. In this lesson, we'll explore some of the features smart pointers have, such as a reference counting smart pointer type. This special type of pointer allows us to keep track of multiple references to data and even clean up when no references remain.

The Box pointer

There might be a scenario where we want to take a simple value, such as a variable declared on the stack, as shown below:

Get hands-on with 1400+ tech skills courses.