Code Example Template
Deep-Dive System Documentation
Raw Pointers: Direct Address Access
Standard Rust references (&T and &mut T) enforce strict borrow-checking rules at compile time to avoid data races. When those constraints are too restrictive (such as interacting with operating system window hooks or handling raw FFI buffers), Rust provides raw pointers:
*const T: Constant raw pointer. Read-only direct address access.*mut T: Mutable raw pointer. Read/Write direct address access.
Crucial Properties
- No Borrow Check: You can have multiple constant and mutable raw pointers pointing to the exact same memory address simultaneously.
- Nullable: Unlike references, raw pointers can be null (
std::ptr::null()). - No Automatic Dereference: Dereferencing a raw pointer to extract or change values must be done inside an
unsafe {}block.
let mut x = 10;
let raw_mut: *mut i32 = &mut x;
unsafe {
*raw_mut = 20; // Manipulates memory directly without borrow checks
}
FFI Mappings
When invoking Windows kernel commands or handling graphics context, window handles are represented as raw void pointers (*const c_void).
Quick Reference Guide
let raw_p: *const i32 = &10;
let val = unsafe { *raw_p };