zoukankan      html  css  js  c++  java
  • 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<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));
    }
    
  • 相关阅读:
    java字符串的遍历以及字符串中各类字符的统计
    Java Jvm运行机制原理
    为什么面试要问 hashmap 的原理
    HashMap的实现原理
    redis两种持久化方式的优缺点
    2018No-java面试知识
    从架构演进的角度聊聊spring cloud都做了些什么?
    MySQL索引优化
    2018java面试知识汇总
    多线程—7种同步方法
  • 原文地址:https://www.cnblogs.com/kwebi/p/9520830.html
Copyright © 2011-2022 走看看