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.

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

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
    202206232210# where the 202206241126# is checked in runtime instead
  • Rust Borrowing Rule

    Rust Borrowing Rule states that there can be either multiple immutable references or only one mutable reference at the same time. If such rule is violated, depending on the situation, the compiler will throw a compilation error or the program will panic during the runtime and crash itself instead (this is the case for #202206232210).

  • 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

    Further inspection into the code, we find out that Mutex offer interior mutability, and can be 202206221604# using the syntax *. It is essentially the same to the 202206232210#. Therefore, one advice is to treat Mutex as if it is a 202111301656# with 202207172210# 202204061235#.

#rust #memory #resource