Rust 智能指针(Rc)

Rust的Rc(Reference Counted)提供单线程的引用计数,允许值有多个所有者。当最后一个Rc实例销毁时,值也会被销毁。Rc不支持可变引用,若需要可变性,可以使用Cell或RefCell。非原子引用计数意味着Rc不能在多线程中使用,适合单线程场景,多线程下可使用Arc。Weak是Rc的弱引用版本,不会增加所有权计数,常用于防止循环引用。
摘要由CSDN通过智能技术生成

我博客园原文链接

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 r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值