DEV Community

Cover image for [Rust Guide] 15.0 Smart Pointer Intro - What Are Smart Pointers and Rust Smart Pointer Traits
SomeB1oody
SomeB1oody

Posted on • Edited on • Originally published at someb1oody.github.io

[Rust Guide] 15.0 Smart Pointer Intro - What Are Smart Pointers and Rust Smart Pointer Traits

15.0.1 Basic Concepts of Pointers

A pointer is a variable that holds an address in memory and points to another piece of data.

The most common pointers in Rust are references, which are written with the & symbol and borrow the value they point to. Aside from referencing data, they have no special behavior and no extra overhead.

15.0.2 An Introduction to Smart Pointers

The concept of smart pointers is not unique to Rust: it originated in C++ and also exists in other languages.

The Rust standard library defines a variety of smart pointers. They provide capabilities beyond what ordinary references can do.

Smart pointers behave like pointers, but they provide extra metadata and functionality.

15.0.3 Differences Between Smart Pointers and References

  • References can only borrow data, while smart pointers usually own the data they point to.
  • Smart pointers have extra metadata and functionality, such as automatic cleanup and other guarantees.

15.0.4 Common Smart Pointer Types

1. Reference Counting Types

Reference-counting smart pointers support multiple ownership by tracking how many owners there are. When there are no more users, they automatically clean up the data.

2. Smart Pointers in the Standard Library

We have already encountered some smart pointers earlier, such as:

  • String: owns a region of memory and guarantees that its data is valid UTF-8.
  • Vec<T>: provides metadata such as capacity and allows you to work with dynamic arrays.

15.0.5 How Smart Pointers Are Implemented

Smart pointers are usually implemented with structs, but unlike ordinary structs, they generally implement two important traits:

  • Deref: allows instances of a smart pointer to behave like references, so programs can support both references and smart pointers.
  • Drop: allows programmers to customize the cleanup code that runs when a smart pointer instance goes out of scope.

In this chapter, we will discuss these two traits and show why they matter for smart pointers.

15.0.6 Chapter Overview

Because smart pointers are a common design pattern, this chapter focuses on the most commonly used smart pointer types in the standard library:

  • Box<T>: the simplest smart pointer, which stores data on the heap.
  • Rc<T>: a reference-counting smart pointer that supports shared ownership.
  • Ref<T> and RefMut<T>: values accessed through RefCell<T>, which enforces the borrow rules at runtime instead of compile time.

In addition, this chapter discusses the following topics:

  • Interior Mutability Pattern: a design pattern that allows an immutable type to expose an API for modifying its internal values.
  • Reference Cycles: how they cause memory leaks and how to prevent them.

By the end of this chapter, you will have a deeper understanding of how smart pointers and related design patterns are used in Rust.

Top comments (0)