Ownership(avoid crash in Rust)
- Defines how memory is managed safely(without need of garbage
collector) by Rust program.
- Every Variable/data have single owner at a time. When the owner goes
out of scope, Rust automatically deallocates the memory.
- Ownership rules are checked at compile time.
- Ownership applies to memory allocated on heap(dynamically), Eg:
String(), vector ie those are moved. For variable allocated on Stack
(Eg: int, float) variable is copied.
In Rust, raw pointers (*const T and *mut T) don't embody ownership
semantics directly. However, smart pointers have ownership like
-
Box<T>: It represents ownership of a heap-allocated value.
-
Rc<T>: It provides reference counting and allows multiple ownership.
- Arc<T>: It's like
Rc<T>, but with atomic reference counting, suitable for concurrent
access.
| C++ (Crash in C++) | Rust (No null pointer Exception(Crash) in Rust) |
unique_ptr
|
|