Manually manage memory through raw pointers.
See also the pointer primitive types.
Many functions in this module take raw pointers as arguments and read from or write to them. For this to be safe, these pointers must be valid. Whether a pointer is valid depends on the operation it is used for (read or write), and the extent of the memory that is accessed (i.e., how many bytes are read/written). Most functions use *mut T
and *const T
to access only a single value, in which case the documentation omits the size and implicitly assumes it to be size_of::<T>()
bytes.
The precise rules for validity are not determined yet. The guarantees that are provided at this point are very minimal:
read_volatile
and write_volatile
: Volatile accesses cannot be used for inter-thread synchronization.These axioms, along with careful use of offset
for pointer arithmetic, are enough to correctly implement many useful things in unsafe code. Stronger guarantees will be provided eventually, as the aliasing rules are being determined. For more information, see the book as well as the section in the reference devoted to undefined behavior.
Valid raw pointers as defined above are not necessarily properly aligned (where "proper" alignment is defined by the pointee type, i.e., *const T
must be aligned to mem::align_of::<T>()
). However, most functions require their arguments to be properly aligned, and will explicitly state this requirement in their documentation. Notable exceptions to this are read_unaligned
and write_unaligned
.
When a function requires proper alignment, it does so even if the access has size 0, i.e., even if memory is not actually touched. Consider using NonNull::dangling
in such cases.
raw_const |
Experimental Create a |
raw_mut |
Experimental Create a |
NonNull |
|
copy⚠ |
Copies |
copy_nonoverlapping⚠ |
Copies |
drop_in_place⚠ |
Executes the destructor (if any) of the pointed-to value. |
eq |
Compares raw pointers for equality. |
hash |
Hash a raw pointer. |
null |
Creates a null raw pointer. |
null_mut |
Creates a null mutable raw pointer. |
read⚠ |
Reads the value from |
read_unaligned⚠ |
Reads the value from |
read_volatile⚠ |
Performs a volatile read of the value from |
replace⚠ |
Moves |
slice_from_raw_parts |
Forms a raw slice from a pointer and a length. |
slice_from_raw_parts_mut |
Performs the same functionality as |
swap⚠ |
Swaps the values at two mutable locations of the same type, without deinitializing either. |
swap_nonoverlapping⚠ |
Swaps |
write⚠ |
Overwrites a memory location with the given value without reading or dropping the old value. |
write_bytes⚠ |
Sets |
write_unaligned⚠ |
Overwrites a memory location with the given value without reading or dropping the old value. |
write_volatile⚠ |
Performs a volatile write of a memory location with the given value without reading or dropping the old value. |
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ptr/index.html