• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Rust智能指针(Rc)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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<Rc>.

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<Rc<T>>
  • 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<Rc<_>> = weak_five.upgrade();
    println!("{}", strong_five.unwrap());
    println!("weak: {}, strong: {}", Rc::weak_count(&five), Rc::strong_count(&five));
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
MATLAB 图像加噪,各种滤波发布时间:2022-07-18
下一篇:
均值滤波去除图像噪声的matlab程序发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap