Unsafe Engineering · 19 min · free lesson

Raw Pointers

Represent an address without borrow guarantees.

Core idea

Creating *const T or *mut T is safe; dereferencing one is unsafe because validity, alignment, initialization, and aliasing are now the programmer's proof obligations.

Coming from C? unsafe, Raw Pointers and FFI covers this side by side.

A raw pointer reaches memory only after allocation, alignment, initialization, bounds, and aliasing requirements are established.
Unsafe dereference requires a concrete proof of the pointer invariants.

Understand it

Creating *const T or *mut T is safe; dereferencing one is unsafe because validity, alignment, initialization, and aliasing are now the programmer's proof obligations.

A raw pointer such as *const T or *mut T can represent a location without the safety guarantees that a normal Rust reference carries. Constructing one from a valid reference is safe. Dereferencing it requires unsafe because the compiler cannot prove that the pointed-to object is still alive, aligned, initialized, in bounds, and accessed under valid aliasing rules. A raw pointer may carry provenance information about its allocation; treating it as merely a numeric address can miss part of the memory model.

In the example, pointer is created from &value, and value remains alive while the pointer is read. That local reasoning supports the unsafe dereference. Change the example conceptually so the pointer outlives value; now the proof fails even if the numeric address still looks plausible. Never use a runtime test as proof that dereferencing an invalid pointer is safe: undefined behavior may appear to work once. Keep the unsafe operation close to the checks that justify it, and expose a safe API only when callers cannot violate those assumptions. For FFI, additionally document who owns the allocation and when the foreign side may retain or free the pointer.

Pointer arithmetic adds more obligations: an offset must stay within the permitted allocation rules even if it is not dereferenced immediately. Do not infer validity from a non-null address alone. Prefer slices, references, and NonNull-based library APIs when they express the invariant safely; a raw pointer is useful at FFI and data-structure internals, where its proof can be tightly contained.

Read the code

let value = 7_u32;
let pointer: *const u32 = &value;
let read = unsafe { *pointer };
println!("{read}");

Creating the pointer from &value is safe; the unsafe block covers only the read, while value is alive.

Practice starter

let dangling: *const u32 = {
    let value = 7_u32;
    &value
};
println!("created {dangling:?}");
// let read = unsafe { *dangling };

Practise this code in Playground ↗

Try it

Decide which lines need unsafe. Then explain why the commented-out read would be undefined behaviour even if it happened to print 7. Do not run it as proof.

What success looks like: You can name the proof the example has and this snippet lacks: value is still alive when the pointer is read.

Check your understanding

Which operation needs unsafe?

Reveal the answer
  1. Dereferencing
  2. Creating the pointer
  3. Printing the value

Creating and printing the pointer are safe, since neither touches the memory behind it. Only the dereference needs unsafe, and here its proof fails.

Go deeper

Common misconception: “Creating a raw pointer is the dangerous step, so it needs an unsafe block.”

Creating *const T or *mut T is safe; only dereferencing one is unsafe, because validity, alignment, initialization, and aliasing are now the programmer's proof obligations.

  1. Create the pointer: &value coerces to *const u32 with no unsafe, because making an address does not touch the memory behind it.
  2. Prove the read: value is alive, aligned, and initialised at *pointer, since the pointer came from a valid reference in the same scope.
  3. Read through it: unsafe { *pointer } copies the u32 out, so read is 7 and println! prints it.

Transfer challenge: Rewrite the example with a *mut u32 built from &mut value, and write through it. Add the SAFETY comment that justifies the write, then predict what printing value shows.

Connections

  • Proc-Macro Boundaries: Separate host-time generation from target code
  • Representation & Layout: Make a data layout contract explicit at a boundary
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Raw Pointers (courseId "systems", lessonId "raw-pointers")
Link: https://crabrix.com/learn/systems/raw-pointers/
Goal: Creating *const T or *mut T is safe; dereferencing one is unsafe because validity, alignment, initialization, and aliasing are now the programmer's proof obligations.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=systems/raw-pointers) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.