The UniquePtr library in Rust is inspired by the unique_ptr smart pointer in C++. Both implementations share the same fundamental purpose: providing unique ownership of dynamically allocated objects and ensuring proper deallocation.
Here are some key similarities and differences between the two:
- Unique Ownership: Both
UniquePtrin Rust andunique_ptrin C++ enforce exclusive ownership of the managed object. This means that only one smart pointer instance can own and manage the object at any given time. - Automatic Deallocation: When the
UniquePtrorunique_ptrinstance goes out of scope or is explicitly reset, they automatically deallocate the managed object, freeing the associated memory.
use unique_ptr::UniquePtr;
fn main() {
let mut num = 100;
let ptr: *mut i32 = &mut num as *mut i32;
let unique = UniquePtr::with_ptr(ptr);
unsafe {
*unique.ptr = 42;
}
assert_eq!(unsafe { *unique.ptr }, 42);
}- Supports
Clone. - Supports
DerefandDerefMut. - Can be used with any type that implements the
Droptrait. - Supports custom deleters using the
Deletertype parameter.
- Implements common smart pointer operations such as resetting the pointer, releasing ownership, cloning the pointer, and dereferencing.
- Provides a safe interfacefor working with raw pointers.
This project is licensed under the MIT license.