Raw Pointers
unsafe, since they skip the usual safety checks.In this page:
let p = &value as *const Type;
let p = &mut value as *mut Type;
unsafe {
let v = *p;
}
Creating Raw Pointers
Raw pointers can be created from references using as *const T or as *mut T. Creating a raw pointer itself is completely safe -- only dereferencing it requires unsafe.
Example: Creating Raw Pointers
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `value`, set to `100`
let value = 100;
// Declare an immutable variable `const_ptr`, set to `&value as *const i32`
let const_ptr = &value as *const i32;
// Print "Pointer created (not yet dereferenced)" to the console (with a trailing newline)
println!("Pointer created (not yet dereferenced)");
unsafe {
// Print "Value: {}" to the console, substituting in `*const_ptr` (with a trailing newline)
println!("Value: {}", *const_ptr);
}
}
Login to try C/C++/Java/PHP code in the editor
Mutable Raw Pointers
*mut T allows modifying the pointed-to data through the pointer, but like all raw pointer dereferences, doing so requires an unsafe block.
Example: Mutable Raw Pointers
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `value`, initialized to `5`
let mut value = 5;
// Declare an immutable variable `mut_ptr`, set to `&mut value as *mut i32`
let mut_ptr = &mut value as *mut i32;
unsafe {
*mut_ptr += 1;
}
// Print "value = {}" to the console, substituting in `value` (with a trailing newline)
println!("value = {}", value);
}
Login to try C/C++/Java/PHP code in the editor
Raw Pointers Skip Borrow Checking
Unlike references, you can have multiple raw pointers (even mutable ones) to the same data at once, since the borrow checker does not track raw pointers -- this is exactly why dereferencing them is unsafe.
Example: Raw Pointers Skip Borrow Checking
// Entry point: execution of the program starts here
fn main() {
// Declare a mutable variable `number`, initialized to `10`
let mut number = 10;
// Declare an immutable variable `p1`, set to `&mut number as *mut i32`
let p1 = &mut number as *mut i32;
// Declare an immutable variable `p2`, set to `&mut number as *mut i32`
let p2 = &mut number as *mut i32;
unsafe {
*p1 += 1;
*p2 += 1;
}
// Print "number = {}" to the console, substituting in `number` (with a trailing newline)
println!("number = {}", number);
}
Login to try C/C++/Java/PHP code in the editor
Why Raw Pointers Exist
Raw pointers are essential for low-level tasks like foreign function interfaces (calling C code) and for building safe abstractions internally, where the implementer manually guarantees the safety invariants the compiler cannot check.
Example: Why Raw Pointers Exist
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `array`, set to `[1, 2, 3, 4]`
let array = [1, 2, 3, 4];
// Declare an immutable variable `ptr`, set to `array.as_ptr()`
let ptr = array.as_ptr();
unsafe {
// Print "First element via raw pointer: {}" to the console, substituting in `*ptr` (with a trailing newline)
println!("First element via raw pointer: {}", *ptr);
// Print "Second element via offset: {}" to the console, substituting in `*ptr.offset(1)` (with a trailing newline)
println!("Second element via offset: {}", *ptr.offset(1));
}
}
Login to try C/C++/Java/PHP code in the editor
- Dereferencing a raw pointer outside of an
unsafeblock, which is a compile error. - Creating a raw pointer to data that has already gone out of scope, resulting in a dangling pointer that is legal to create but unsafe to dereference.
- Confusing
*const T(immutable raw pointer) with*mut T(mutable raw pointer) -- they have different capabilities.
*const Tand*mut Tare raw pointers that bypass Rust's normal borrowing rules.- Creating a raw pointer is safe; dereferencing one requires an
unsafeblock. - Raw pointers can be null, dangling, or unaligned -- Rust does not guarantee their validity the way it does for references.
- Raw pointers are mainly used for low-level interoperability (like FFI) and building custom safe abstractions.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: