Crates.io | unique_ptr |
lib.rs | unique_ptr |
version | 0.1.3 |
source | src |
created_at | 2023-07-12 16:40:27.67809 |
updated_at | 2023-07-12 18:11:03.541684 |
description | A simple smart pointer implementation in Rust. |
homepage | https://github.com/stescobedo92/default_deleter |
repository | https://github.com/stescobedo92/default_deleter |
max_upload_size | |
id | 914618 |
size | 6,538 |
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:
UniquePtr
in Rust and unique_ptr
in 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.UniquePtr
or unique_ptr
instance 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);
}
Clone
.Deref
and DerefMut
.Drop
trait.Deleter
type parameter.This project is licensed under the MIT license.