liveBook · Manning

Type state

The typestate pattern encodes an object's valid states and permitted operations or transitions in its types, allowing invalid uses to be rejected by the type system.

How 3 Manning books explain it

Write Powerful Rust Macros

The type state pattern encodes an object's permitted states in its type so invalid state transitions or configurations are rejected at compile time.

How it is used
Enforces required builder fields and valid state transitions before construction or final macro output.
How deep it goes
Works through marker traits, generic state parameters, PhantomData, generated transition methods, and compile-fail testing for a builder.
Examples
Uses a red/green traffic light, a Gleipnir builder with three required fields, and a missing-field compile-fail test.
What you can do after
Build it from scratch by generating state-specific builders whose final state alone exposes `build`.

Read how Write Powerful Rust Macros explains type state →

Idiomatic Rust

A type-safe state machine is a state-machine design whose valid states and transitions are enforced by the programming language's type system.

How it is used
Uses type-level states to keep software and external state aligned and make invalid transitions unavailable.
How deep it goes
Defines states and transitions and demonstrates state-specific methods; omits database lookup and validation during session resumption.
Examples
Demonstrates light-bulb transitions and a user session with authentication, property updates, and logout.
What you can do after
Build a Rust state machine whose state-specific methods and transitions are checked by the type system.

Read how Idiomatic Rust explains type state →

Embedded Software with Rust

The typestate pattern represents an object's state in its type so that operations can be restricted to valid states at compile time.

How it is used
Prevents invalid peripheral operations by encoding hardware configuration in Rust types.
How deep it goes
Explains type-level modes, trait-limited operations, ownership-based pin conversion, and validated clock states.
Examples
Demonstrates UART receive versus SPI misuse, GPIO pin-mode conversion, and frozen clocks for SysTick delays.
What you can do after
Apply it through a HAL to configure pins and clocks while catching invalid operations at compile time.

Read how Embedded Software with Rust explains type state →

What to know first

Related concepts

Questions about type state

What is type state?

The typestate pattern encodes an object's valid states and permitted operations or transitions in its types, allowing invalid uses to be rejected by the type system.

Which Manning books explain type state?

Write Powerful Rust Macros, Idiomatic Rust and Embedded Software with Rust.

What should I know before learning type state?

Manning books that teach type state usually introduce macro and trait first.

What is type state a kind of?

Type state is a kind of finite-state machine. A finite-state machine is a computational model with a finite set of states and defined transitions between them, typically determined by inputs or events.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.