Abstractions in Practice · 19 min · free lesson

Phantom Types

Track a compile-time state with no stored value.

Core idea

PhantomData tells the type system that a zero-sized marker matters to ownership or state.

Diagram for Phantom Types: a Rust code example flows to the lesson rule and the key observation.
The example makes the contract visible in code.

Understand it

PhantomData tells the type system that a zero-sized marker matters to ownership or state. It enables APIs where illegal transitions have no callable method, without adding runtime storage.

PhantomData<T> is a zero-sized marker that tells the compiler a type is logically related to T even when it stores no T value. In Job<State>, the state appears only at the type level. Job<Ready> and Job<Running> can have the same runtime fields but expose different methods. A transition can consume one state and return another, so calling a finish method on a job that was never started becomes a compile-time error.

PhantomData<T> itself has guaranteed size zero and alignment one for every T. The containing structure still follows its own layout rules. The marker has no runtime payload, but it is not decorative. It affects how the compiler reasons about ownership, variance, drop checking, and automatic traits in more advanced code. That is why the choice of marker type matters in low-level APIs. In a beginner typestate design, the central idea is simpler: the state is represented by a type rather than a boolean field that every method must check at runtime.

Sketch a start(self) -> Job<Running> method defined only for Job<Ready>, then a finish(self) -> Job<Done> defined only for Job<Running>. Try calling finish on the original ready job and inspect the method-not-found error. Notice that each transition consumes its old state, preventing two parallel versions of the same logical job from being used accidentally. Use this pattern when invalid transitions are costly enough to justify the extra types; a small ordinary enum may be clearer for a simple runtime choice.

Read the code

use std::marker::PhantomData;
struct Ready;
struct Job<State> {
    id: u32,
    state: PhantomData<State>,
}

The example makes the contract visible in code.

Practise this code in Playground ↗

Try it

Say what Job<Ready> stores at runtime, and what the state field is for.

What success looks like: You can explain why PhantomData<State> is needed, and what it costs at runtime.

Check your understanding

What layout is guaranteed for PhantomData<T> itself?

Reveal the answer
  1. Size zero and alignment one for every T
  2. One pointer, regardless of T
  3. The same size and alignment as T

PhantomData<T> is guaranteed to have size 0 and alignment 1 for every T. Its ownership, variance and auto-trait effects still matter; the containing structure has its own layout.

Go deeper

Common misconception: “PhantomData<T> stores a T, or at least a pointer to one.”

It stores nothing: PhantomData<T> has size zero and alignment one for every T. The state field exists only for the type system, so Job<Ready> is as large as id alone.

  1. Find the state: Job<State> stores only id: u32. Ready has no fields, and state: PhantomData<State> puts the type parameter to use with no stored State.
  2. Predict before checking: Remove the state field and predict what the compiler says about State. Then restore it and predict std::mem::size_of::<Job<Ready>>().
  3. Compare with evidence: Without the field you get E0392, because State is never used. With it, the size is 4: PhantomData adds no bytes.

Transfer challenge: Add struct Running;, an impl Job<Ready> with start(self) -> Job<Running>, and an impl Job<Running> with finish(self). Call finish on a ready job and read E0599.

Connections

  • Closure Capture Modes: Infer shared, mutable, or moved captures
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: Phantom Types (courseId "ownership", lessonId "phantom-data")
Link: https://crabrix.com/learn/ownership/phantom-data/
Goal: PhantomData tells the type system that a zero-sized marker matters to ownership or state.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/phantom-data) 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.