Primitive Type pointer
Expand description
Raw, unsafe pointers, *const T, and *mut T.
Working with raw pointers in Rust is uncommon, typically limited to a few patterns. Raw pointers
can be out-of-bounds, unaligned, or null. However, when loading from or storing to a raw
pointer, it must be valid for the given access and aligned. When using a field expression,
tuple index expression, or array/slice index expression on a raw pointer, it follows the rules
of in-bounds pointer arithmetic.
Storing through a raw pointer using *ptr = data calls drop on the old value, so
write must be used if the type has drop glue and memory is not already
initialized - otherwise drop would be called on the uninitialized memory.
Use the null and null_mut functions to create null pointers, and the
is_null method of the *const T and *mut T types to check for null.
The *const T and *mut T types also define the offset method, for
pointer math.
§Common ways to create raw pointers
§1. Coerce a reference (&T) or mutable reference (&mut T).
let my_num: i32 = 10;
let my_num_ptr: *const i32 = &my_num;
let mut my_speed: i32 = 88;
let my_speed_ptr: *mut i32 = &mut my_speed;To get a pointer to a boxed value, dereference the box:
let my_num: Box<i32> = Box::new(10);
let my_num_ptr: *const i32 = &*my_num;
let mut my_speed: Box<i32> = Box::new(