Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help


x-i18n: generated_at: “2026-03-01T14:30:34Z” model: gemini-3-flash-preview provider: google-gemini-cli source_hash: adf324198a3eb791ba5828cee69a201f3f2e6f8493280bfb5c897e8b50fca0f1 source_path: ch15-00-smart-pointers.md workflow: 16

智能指针 (Smart Pointers)

Smart Pointers

指针是一个通用概念,指包含内存地址的变量。该地址引用或“指向”某些其他数据。Rust 中最常见的指针类型是引用,你在第 4 章中已经学习过。引用由 & 符号指示,并借用它们指向的值。除了引用数据之外,它们没有其他特殊功能,也没有额外开销。

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 & symbol and borrow the value they point to. They don’t have any special capabilities other than referring to data, and they have no overhead.

另一方面,“智能指针 (smart pointers)”是表现得像指针,但同时也具有额外的元数据和功能的数据结构。智能指针的概念并非 Rust 特有:智能指针起源于 C++,也存在于其他语言中。Rust 标准库中定义了多种智能指针,提供引了用所不具备的功能。为了探索这个通用概念,我们将看几个不同的智能指针示例,包括“引用计数 (reference counting)”智能指针类型。这种指针允许通过跟踪所有者的数量来使数据拥有多个所有者,并在没有所有者剩余时清理数据。

Smart pointers, on the other hand, are data structures that act like a pointer but also have additional metadata and capabilities. The concept of smart pointers isn’t 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, we’ll look at a couple of different examples of smart pointers, including a reference counting 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.

在拥有所有权和借用概念的 Rust 中,引用和智能指针之间还有一个额外的区别:引用仅借用数据,而在许多情况下,智能指针“拥有”它们指向的数据。

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 own the data they point to.

智能指针通常使用结构体来实现。与普通结构体不同,智能指针实现了 DerefDrop 特征。Deref 特征允许智能指针结构体的实例表现得像引用一样,这样你编写的代码就可以同时适用于引用或智能指针。Drop 特征允许你自定义当智能指针实例超出作用域时运行的代码。在本章中,我们将讨论这两个特征,并演示它们对智能指针的重要性。

Smart pointers are usually implemented using structs. Unlike an ordinary struct, smart pointers implement the Deref and Drop traits. The Deref 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 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, we’ll discuss both of these traits and demonstrate why they’re important to smart pointers.

鉴于智能指针模式是 Rust 中经常使用的通用设计模式,本章不会涵盖每一种现有的智能指针。许多库都有自己的智能指针,你甚至可以编写自己的。我们将涵盖标准库中最常见的智能指针:

Given that the smart pointer pattern is a general design pattern used frequently in Rust, this chapter won’t cover every existing smart pointer. Many libraries have their own smart pointers, and you can even write your own. We’ll cover the most common smart pointers in the standard library:

  • Box<T>,用于在堆上分配值

  • Rc<T>,一种支持多重所有权的引用计数类型

  • Ref<T>RefMut<T>,通过 RefCell<T> 访问,这是一种在运行时而非编译时强制执行借用规则的类型

  • Box<T>, for allocating values on the heap

  • Rc<T>, a reference counting type that enables multiple ownership

  • Ref<T> and RefMut<T>, accessed through RefCell<T>, a type that enforces the borrowing rules at runtime instead of compile time

此外,我们还将介绍“内部可变性 (interior mutability)”模式,即一个不可变类型暴露一个用于修改内部值的 API。我们还将讨论引用循环:它们如何导致内存泄漏,以及如何防止它们。

In addition, we’ll cover the interior mutability pattern where an immutable type exposes an API for mutating an interior value. We’ll also discuss reference cycles: how they can leak memory and how to prevent them.

让我们开始吧!

Let’s dive in!