Javatpoint Logo
Javatpoint Logo

Rust Smart Pointers

  • A Smart Pointer is a data structure that behaves like a pointer while providing additional features such as memory management or bound checking.
  • Smart Pointers keep track of the memory that it points to, and is also used to manage other resources such as Fils handles and network connections.
  • Smart pointers were first used in the C++ language.
  • Reference is also a kind of pointer, but it does not have additional capabilities other than referring to the data. Reference is represented by '&' operator.
  • A Smart Pointer provides the additional functionalities beyond that provided by the reference. The most common feature that smart pointer provides "reference counting smart pointer type". This feature enables us to have multiple owners of data by keeping track of the owners, and if no owner remains, then it cleans up the data.
  • References are the pointers that only borrow the data while smart pointers are the pointers that own the data they point to.

Types of Smart pointers:

rust Smart Pointers
  • Box<T>: The Box<T> is a smart pointer which points to the data allocated on the heap of type T where 'T' is the type of the data. It is used to store the data on the heap rather than on the stack.
  • Deref<T>: The Deref<T> is a smart pointer which is used to customize the behavior of the dereference operator(*).
  • Drop<T>: The Drop<T> is a smart pointer used to free the space from the heap memory when the variable goes out of the scope.
  • Rc<T>: The Rc<T> stands for reference counted pointer. It is a smart pointer which keeps a record of the number of references to a value stored on the heap.
  • RefCell<T>: The RefCell<T> is a smart pointer which allows you to borrow the mutable data even if the data is immutable. This process is known as interior mutability.

Next TopicRust Box<T>




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials