Rust Smart Pointers

Rust standard library implements several smart pointers for better resource cleaned up as they have implemented the 202206221653# 202204061235#. If a reference go out of scope, depending on which kind of smart pointer that it is using, either it will be automatically destroyed or decrements the number of references that is pointing to the referred variable or object. Unlike other programming languages, such rule can be check in compile time if wanted.

All smart pointers have 202206221604# trait implemented so that they could be treated like any other reference type in Rust. Meaning, they can use * and & without any significance of syntactic difference from references.

The following shows several smart pointer implementation available in Rust:

Note: Except Rc<T>, all smart pointers will move the variable which cause a transfer of the ownership.

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

  • Rust Drop

    Drop is a #202204061235 which could be used for automatic resource clean up# when the object or variable is out of scope. It is typically implemented in #202111301656. To implement the trait Drop, we need to implement the method drop for the structure:

  • Rust Deref

    Deref is a #202204061235 that allows the dereferencing of a #data-structure or an enum. It is especially useful in #202111301656. All structures that implement the trait Deref must have the method deref implemented into them. This is shown as follow:

  • Rust Box

    Box<T> is a kind of #202111301656 implemented by Rust standard library which is actually a single element Tuple with 202206221604# and Drop 202204061235#. It doesn’t have performance overhead. It stores data on the 202202062259# rather than on the 202112031157#. We can directly print the value (if it is of simple type) just like other primitive types without dereferencing it. Comparing to normal references, box is pointing to a copied value.

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

  • 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 #resource