Rust 智能指针(Rc)

我博客园原文链接

std::rc::Rc

Rc代表引用计数

以下是标准库文档的介绍

Single-threaded reference-counting pointers. ‘Rc’ stands for ‘Reference Counted’.
The type Rc provides shared ownership of a value of type T, allocated in the heap. Invoking clone on Rc produces a new pointer to the same value in the heap.
When the last Rc pointer to a given value is destroyed, the pointed-to value is also destroyed.
Shared references in Rust disallow mutation by default, and Rc is no exception: you cannot generally obtain a mutable reference to something inside an Rc. If you need mutability, put a Cell or RefCell inside the Rc; see an example of mutability inside an Rc.
Rc uses non-atomic reference counting. This means that overhead is very low, but an Rc cannot be sent between threads, and consequently Rc does not implement Send. As a result, the Rust compiler will check at compile time that you are not sending Rcs between threads. If you need multi-threaded, atomic reference counting, use sync::Arc.

总结为以下几点:
+ Rc让一个值有多个所有者,调用clone产生一个指针指向该值
+ 当Rc指针全部销毁时,该值也销毁
+ 不能通过Rc获得可变引用
+ Rc是非原子引用,只能用于单线程,多线程用Arc

use std::rc::Rc;

fn main() {
    let five = Rc::new(5);
    let five1 = five.clone();
    //Rc实现了Deref trait,可以自动解引用,因此下面打印成功
    println!("{}", five1);
    //可以通过调用strong_count查看引用计数
    println!("{}", Rc::strong_count(&five1));
}

顺便介绍一下rc::Weak

标准库介绍

Weak is a version of Rc that holds a non-owning reference to the managed value. The value is accessed by calling upgrade on the Weak pointer, which returns an Option.

Since a Weak reference does not count towards ownership, it will not prevent the inner value from being dropped, and Weak itself makes no guarantees about the
value still being present and may return None when upgraded.

A Weak pointer is useful for keeping a temporary reference to the value within Rc without extending its lifetime. It is also used to prevent circular references between Rc pointers, since mutual owning references would never allow either Rc to be dropped. For example, a tree could have strong Rc pointers from parent nodes to children, and Weak pointers from children back to their parents.

The typical way to obtain a Weak pointer is to call Rc::downgrade.

  • Weak用于不拥有所有权的引用,通过调用upgrade调用值,这个方法返回一个Optical>
  • Weak不能阻止值被丢弃,当值被丢弃时,调用upgrade时返回None
  • 使用Weak可以避免Rc的循环引用
  • 调用Rc::downgrade来获得一个Weak指针
use std::rc::Rc;

fn main() {
    let five = Rc::new(5);
    let weak_five = Rc::downgrade(&five);
    let strong_five: Option_>> = weak_five.upgrade();
    println!("{}", strong_five.unwrap());
    println!("weak: {}, strong: {}", Rc::weak_count(&five), Rc::strong_count(&five));
}

你可能感兴趣的:(Rust 智能指针(Rc))