Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Raw pointers in Rust are unmanaged memory addresses that bypass the compiler’s borrow checker and memory safety guarantees. They are represented by two primitive types: *const T (immutable raw pointer) and *mut T (mutable raw pointer). Unlike standard Rust references (&T and &mut T), raw pointers are not bound by lifetime restrictions and do not enforce aliasing rules at compile time.

Core Characteristics

Raw pointers strip away Rust’s standard safety invariants. Specifically, they:
  • Are not guaranteed to be valid: They can point to unallocated memory, freed memory (dangling pointers), or unaligned memory.
  • Can be null: Unlike safe references, raw pointers can represent a null address.
  • Lack lifetime tracking: The compiler does not track how long the data behind a raw pointer remains valid.
  • Allow aliasing: You can have multiple *mut T pointers to the same memory location, or a mix of *const T and *mut T, bypassing the strict single-mutable-reference rule.
  • Do not implement Drop: When a raw pointer goes out of scope, the memory it points to is not automatically deallocated or cleaned up.

Creating Raw Pointers

Creating a raw pointer is a safe operation. The compiler allows you to instantiate raw pointers because merely holding a memory address does not violate memory safety; the danger lies in accessing the data at that address. From Safe References: You can create raw pointers by casting safe references using the as keyword.
let x = 42;
let mut y = 10;

// Casting an immutable reference to an immutable raw pointer
let ptr_const: *const i32 = &x as *const i32;

// Casting a mutable reference to a mutable raw pointer
let ptr_mut: *mut i32 = &mut y as *mut i32;
From Memory Addresses: Raw pointers can be created directly from integer types representing memory addresses.
let address = 0x012345usize;
let ptr: *const i32 = address as *const i32;
Using the ptr Module: The standard library provides functions to construct raw pointers, such as creating explicit null pointers.
use std::ptr;

let null_ptr: *const i32 = ptr::null();
let null_mut_ptr: *mut i32 = ptr::null_mut();

Dereferencing Raw Pointers

While creating a raw pointer is safe, dereferencing it (accessing or modifying the data it points to) is an unsafe operation. By using an unsafe block, the programmer assumes full responsibility for ensuring the pointer is valid, properly aligned, and does not violate data race rules. Dereferencing requires the * operator.
let mut data = 100;
let ptr: *mut i32 = &mut data as *mut i32;

unsafe {
    // Reading via raw pointer
    let value = *ptr; 
    
    // Writing via raw pointer
    *ptr = value + 50; 
}

Pointer Arithmetic

Raw pointers do not support standard arithmetic operators (like + or -) directly. This is a deliberate design choice to prevent accidental out-of-bounds memory access. Instead, pointer arithmetic is performed using explicit methods provided by the pointer types. Because calculating an out-of-bounds address triggers Undefined Behavior, standard arithmetic methods such as offset, add, and sub are unsafe functions and must be called within an unsafe block. The only exception to this rule is wrapping arithmetic (e.g., wrapping_add, wrapping_sub, wrapping_offset), which is safe to call because it wraps around at the edge of the address space instead of triggering Undefined Behavior.
let array = [10, 20, 30];
let ptr: *const i32 = array.as_ptr();

// Safe: Wrapping arithmetic does not trigger UB on out-of-bounds calculations
let safe_ptr = ptr.wrapping_add(1);

unsafe {
    // Unsafe: add() requires the resulting pointer to remain within the bounds 
    // of the same allocated object to avoid Undefined Behavior.
    let second_element_ptr = ptr.add(1);
    
    // Dereferencing the calculated pointer is also unsafe
    let value = *second_element_ptr; // 20
}
Master Rust with Deep Grasping Methodology!Learn More