Files
docs-rust/ch15/ch15-00-smart-pointers.html
2026-06-22 21:27:36 +05:30

50 lines
3.1 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smart Pointers</title>
</head>
<body>
<h1 id="smart-pointers"><a class="header" href="#smart-pointers">Smart Pointers</a></h1>
<p>A pointer is a general concept for a variable that contains an address in
memory. This address refers to, or “points at,” some other data. The most
common kind of pointer in Rust is a reference, which you learned about in
Chapter 4. References are indicated by the <code>&amp;</code> symbol and borrow the value they
point to. They dont have any special capabilities other than referring to
data, and they have no overhead.</p>
<p><em>Smart pointers</em>, on the other hand, are data structures that act like a
pointer but also have additional metadata and capabilities. The concept of
smart pointers isnt unique to Rust: Smart pointers originated in C++ and exist
in other languages as well. Rust has a variety of smart pointers defined in the
standard library that provide functionality beyond that provided by references.
To explore the general concept, well look at a couple of different examples of
smart pointers, including a <em>reference counting</em> smart pointer type. This
pointer enables you to allow data to have multiple owners by keeping track of
the number of owners and, when no owners remain, cleaning up the data.</p>
<p>In Rust, with its concept of ownership and borrowing, there is an additional
difference between references and smart pointers: While references only borrow
data, in many cases smart pointers <em>own</em> the data they point to.</p>
<p>Smart pointers are usually implemented using structs. Unlike an ordinary
struct, smart pointers implement the <code>Deref</code> and <code>Drop</code> traits. The <code>Deref</code>
trait allows an instance of the smart pointer struct to behave like a reference
so that you can write your code to work with either references or smart
pointers. The <code>Drop</code> trait allows you to customize the code thats run when an
instance of the smart pointer goes out of scope. In this chapter, well discuss
both of these traits and demonstrate why theyre important to smart pointers.</p>
<p>Given that the smart pointer pattern is a general design pattern used
frequently in Rust, this chapter wont cover every existing smart pointer. Many
libraries have their own smart pointers, and you can even write your own. Well
cover the most common smart pointers in the standard library:</p>
<ul>
<li><code>Box&lt;T&gt;</code>, for allocating values on the heap</li>
<li><code>Rc&lt;T&gt;</code>, a reference counting type that enables multiple ownership</li>
<li><code>Ref&lt;T&gt;</code> and <code>RefMut&lt;T&gt;</code>, accessed through <code>RefCell&lt;T&gt;</code>, a type that enforces
the borrowing rules at runtime instead of compile time</li>
</ul>
<p>In addition, well cover the <em>interior mutability</em> pattern where an immutable
type exposes an API for mutating an interior value. Well also discuss
reference cycles: how they can leak memory and how to prevent them.</p>
<p>Lets dive in!</p>
</body>
</html>