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