Reference Counting Pointer

Rc<T> is a kind of the #202111301656 that share the ownership of the referred variable or object between multiple pointers. Each time when the reference is “borrowed”, it will increment the reference count (using method Rc::clone), and if such count is down to 0, Rc<T> will be destroyed#. This is similar to #cpp shared_ptr with the difference that it allows only reading due to its immutable nature.

Note: We can inspect the current reference count by printing the return value from the class method Rc::strong_count(&{Rc<T>}).

If there is a case where ownership should be not owned by other but sharing is still necessary (to avoid 202206251349#), use the method Rc::downgrade to get a 202206251329#. We can track how many Weak<T> are in the scope using Rc::weak_count(&{Rc<T>}). Unlike strong_count, Rc<T> will be clean up regardless of the value of weak_count since Weak<T> doesn’t possess the ownership of the reference.

Rc<T> can’t be used in a multithreading environment# as it doesn’t implement the 202204061235# 202207172159# and 202207172210#.

Links to this page
  • Rust Weak Pointer

    Weak<T> is a kind of #202111301656 that doesn’t possess ownership of a variable or an object and avoid 202206251349# that could be created from #202206232210 (especially RefCell<Rc<T>>) by replacing #202206221720. It can be obtained from Rc<T> with the method Rc::downgrade. All Weak<T> will be clean up once they are out of scope, regardless of the value of weak_count (from Rc<T>).

  • Rust Sync

    Sync is a #202204061235 that allows the type to be referenced in a multithreading environment#. Rust primitive types has this trait implemented by default, and same as 202207172159, if the type is entirely composed of these types, then it will have this trait implicitly. Some types such as raw pointers, #202206221720 and Cell<T> related types (including #202206232210) doesn’t implement this trait.

  • Rust Smart Pointers
    202206221720# that allow multiple ownership of the same object
  • Rust Send

    Send is a #202204061235 that allows the ownership of implemented types to be transferred safely in a multithreading environment#. Almost all Rust types are implemented with Send trait, except #202206221720, and if the user’s custom type is composed of those types, then it will automatically have the Send trait. There is no method needed to be implemented in this trait.

  • Rust RefCell

    RefCell<T> (import it from std::cell::RefCell) is a kind of #202111301656 that implements single ownership but allows interior mutability which permits the mutation of data on immutable references. Unlike 202206221113 and 202206221720, it checks the 202206241126# in runtime rather in compile time. This means that instead of showing up as a compilation error, the program will still be able to be compiled but panic and exit if there is a violation of the borrowing rule.

    RefCell<T> can combine with 202206221720 to allow multiple ownership on mutable reference. That being said, be aware of the possibility of having 202206251349#.

  • Reference Cycle

    Reference Cycle is a phenomenon in Rust when the reference count from #202206221720 can’t go down to 0 because references are referring or pointing to each other. Since the reference count of Rc<T> is never going to be 0, it will not be clean up by Rust in runtime, in turn cause memory leak (unused memory). This is quite common when the codebase use 202206232210# with a combination of Rc<T> (in the form of RefCell<Rc<T>>).

  • Mutex in Rust

    As you can see that, we wrap around the Mutex into another type, Arc (imported from std::sync::Arc). It allows the data to be shared across multiple threads without violating the rule of Rust ownership, that is, it is atomic. You can treat it as 202206221720# with the 202207172159# and 202207172210# 202204061235#.

#rust #resource #cpp #)