PhantomData and typestate
Quick answer
Typestate uses different types for different legal states. PhantomData lets a type carry a compile-time marker without storing a marker value with runtime size.
Encode transitions in signatures
A method consuming Door<Closed> can return Door<Open>. Methods available only on Open cannot be called on Closed. PhantomData<S> is zero-sized but affects type-system properties including variance and auto traits. It does not by itself establish resource validity; constructors and transition implementations must uphold the invariant.
use std::marker::PhantomData;struct Closed; struct Open;struct Door<S>(PhantomData<S>);impl Door<Closed> { fn open(self) -> Door<Open> { Door(PhantomData) } }impl Door<Open> { fn enter(&self) { println!("enter"); } }fn main() { Door::<Closed>(PhantomData).open().enter(); }Expected stdout (not executed):
enterConsuming the old state prevents reuse of that particular value.
use std::marker::PhantomData;struct Closed; struct Open;struct Door<S>(PhantomData<S>);impl Door<Open> { fn enter(&self) {} }fn main() { Door::<Closed>(PhantomData).enter(); }Expected: compilation fails; exact diagnostic not verified.
The invalid operation is rejected before execution.
Common mistake
Public unchecked constructors can bypass the intended state machine.
Try it yourself
Add a consuming close method for Door<Open> that returns Door<Closed>.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
PhantomData and typestate — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A workflow must prevent publishing an unreviewed article at compile time. Which design fits?
PhantomData and typestate — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::marker::PhantomData; struct Closed; struct Open; struct Door<S>(PhantomData<S>); impl Door<Closed> { fn open(self) -> Door<Open> { Door(PhantomData) } } impl Door<Open> { fn enter(&self) { println!("enter"); } } fn main() { Door::<Closed>(PhantomData).open().enter(); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
PhantomData and typestate — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::marker::PhantomData; struct Closed; struct Open; struct Door<S>(PhantomData<S>); impl Door<Open> { fn enter(&self) {} } fn main() { Door::<Closed>(PhantomData).enter(); } ``` Which outcome or interpretation best matches this example?