> ## 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.

# Rust Raw Pointer

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.

```rust theme={"dark"}
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.

```rust theme={"dark"}
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.

```rust theme={"dark"}
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.

```rust theme={"dark"}
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.

```rust theme={"dark"}
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
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Rust Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
