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.
It provides two major methods: borrow_mut
and borrow
. borrow_mut
will return a mutable reference (RefMut<T>
) where one could alter the content of the referred variable there. borrow
returns an immutable reference (Ref<T>
) instead. RefCell<T>
keeps track on how many RefMut<T>
and Ref<T>
are currently active, and panic the program if it detected abnormal occurrences of them that violate 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#.
However, it can’t be used in a multithreading environment# as it doesn’t implement the 202204061235# 202207172210#.