Tutorial by Examples

// Let's take an arbitrary piece of data, a 4-byte integer in this case let some_data: u32 = 14; // Create a constant raw pointer pointing to the data above let data_ptr: *const u32 = &some_data as *const u32; // Note: creating a raw pointer is totally safe but dereferencing a raw pointe...
// Let's take a mutable piece of data, a 4-byte integer in this case let mut some_data: u32 = 14; // Create a mutable raw pointer pointing to the data above let data_ptr: *mut u32 = &mut some_data as *mut u32; // Note: creating a raw pointer is totally safe but dereferencing a raw pointe...
Unlike normal Rust references, raw pointers are allowed to take null values. use std::ptr; // Create a const NULL pointer let null_ptr: *const u16 = ptr::null(); // Create a mutable NULL pointer let mut_null_ptr: *mut u16 = ptr::null_mut();
Just like in C, Rust raw pointers can point to other raw pointers (which in turn may point to further raw pointers). // Take a regular string slice let planet: &str = "Earth"; // Create a constant pointer pointing to our string slice let planet_ptr: *const &str = &planet ...
Rust has a default formatter for pointer types that can be used for displaying pointers. use std::ptr; // Create some data, a raw pointer pointing to it and a null pointer let data: u32 = 42; let raw_ptr = &data as *const u32; let null_ptr = ptr::null() as *const u32; // the {:p} mappi...

Page 1 of 1