Smart pointers

Managing raw pointers is highly unsafe and developers need to be careful about a lot of details when using them. Uninformed usage may lead to issues such as memory leaks, dangling references, and double frees in large code bases in non-obvious ways. To alleviate from these issues, we can use smart pointers, which were popularized by C++.

Rust also has many kinds of smart pointers. They are called smart because they also have extra metadata and code associated with them that gets executed when they are created or destroyed. Being able to automatically free the underlying resource when a smart pointer goes out of scope is one of the major reasons to use smart pointers.

Much of the smartness in smart pointers comes from two traits, called the Drop trait and the Deref trait. Before we explore the available smart pointer types in Rust, let's understand these traits in detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset