11-引用计数

引用计数 — Rust 语言之旅

内容来源 · Tour of Rust / Rust 语言之旅

原文链接: https://tourofrust.com/100_zh-cn.html

引用计数

Rc 是一个能将数据从栈移动到智能指针。 它允许我们克隆其他Rc智能指针,这些指针都具有不可改变地借用放在堆上的数据的能力。

只有当最后一个智能指针被删除时,堆上的数据才会被释放。

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use std::rc::Rc;

struct Pie;

impl Pie {
    fn eat(&self) {
        println!("tastes better on the heap!")
    }
}

fn main() {
    let heap_pie = Rc::new(Pie);
    let heap_pie2 = heap_pie.clone();
    let heap_pie3 = heap_pie2.clone();

    heap_pie3.eat();
    heap_pie2.eat();
    heap_pie.eat();

    // all reference count smart pointers are dropped now
    // the heap data Pie finally deallocates
}
最后修改 August 21, 2026: 更新 (76fc81a2e)