Smart Pointers in Rust
Rust is considered safe because it makes sure variable ownership is managed correctly in our code. In the most basic case, Rust enforces these rules:
- Each value in Rust has an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped.
The problem is that there are some scenarios where we need to break these rules. This is where smart pointers help us.
What are smart pointers?
Smart pointers are structs that manage some internal data.
They are called pointers because they implement the Deref trait, so they can be used like pointers (Using the &
and *
syntax).