Module std::ptr[src]
Operations on unsafe pointers, *T
, and *mut T
.
Working with unsafe pointers in Rust is uncommon, typically limited to a few patterns.
Use the null
function to create null pointers,
the is_null
and is_not_null
methods of the RawPtr
trait to check for null.
The RawPtr
trait is imported by the prelude, so is_null
etc.
work everywhere. The RawPtr
also defines the offset
method,
for pointer math.
Common ways to create unsafe pointers
1. Coerce a reference (&T
) or mutable reference (&mut T
).
fn main() {
let my_num: int = 10;
let my_num_ptr: *int = &my_num;
let mut my_speed: int = 88;
let my_speed_ptr: *mut int = &mut my_speed;
}let my_num: int = 10; let my_num_ptr: *int = &my_num; let mut my_speed: int = 88; let my_speed_ptr: *mut int = &mut my_speed;
This does not take ownership of the original allocation and requires no resource management later, but you must not use the pointer after its lifetime.
2. Transmute an owned box (Box<T>
).
The transmute
function takes, by value, whatever it's given
and returns it as whatever type is requested, as long as the
types are the same size. Because Box<T>
and *T
have the same
representation they can be trivially,
though unsafely, transformed from one type to the other.
use std::mem; unsafe { let my_num: Box<int> = box 10; let my_num: *int = mem::transmute(my_num); let my_speed: Box<int> = box 88; let my_speed: *mut int = mem::transmute(my_speed); // By taking ownership of the original `Box<T>` though // we are obligated to transmute it back later to be destroyed. drop(mem::transmute::<_, Box<int>>(my_speed)); drop(mem::transmute::<_, Box<int>>(my_num)); }
Note that here the call to drop
is for clarity - it indicates
that we are done with the given value and it should be destroyed.
3. Get it from C.
extern crate libc; use std::mem; fn main() { unsafe { let my_num: *mut int = libc::malloc(mem::size_of::<int>() as libc::size_t) as *mut int; if my_num.is_null() { fail!("failed to allocate memory"); } libc::free(my_num as *mut libc::c_void); } }extern crate libc; use std::mem; fn main() { unsafe { let my_num: *mut int = libc::malloc(mem::size_of::<int>() as libc::size_t) as *mut int; if my_num.is_null() { fail!("failed to allocate memory"); } libc::free(my_num as *mut libc::c_void); } }
Usually you wouldn't literally use malloc
and free
from Rust,
but C APIs hand out a lot of pointers generally, so are a common source
of unsafe pointers in Rust.
Traits
RawPtr | Methods on raw pointers |
Functions
array_each | Given a null-pointer-terminated **T (pointer to an array of pointers), iterate through each *T, passing to the provided callback function |
array_each_with_len | Given a **T (pointer to an array of pointers),
iterate through each *T, up to the provided |
buf_len | Return the offset of the first null pointer in |
copy_memory | Copies data from one location to another. |
copy_nonoverlapping_memory | Copies data from one location to another. |
mut_null | Create an unsafe mutable null pointer. |
null | Create a null pointer. |
position | Return the first offset |
read | Reads the value from |
read_and_zero | Reads the value from |
replace | Replace the value at a mutable location with a new one, returning the old value, without deinitialising either. |
set_memory | Invokes memset on the specified pointer, setting |
swap | Swap the values at two mutable locations of the same type, without deinitialising either. They may overlap. |
write | Unsafely overwrite a memory location with the given value without destroying the old value. |
zero_memory | Zeroes out |