liveBook · Manning

Marker trait

A marker trait is a trait without methods or associated properties that classifies implementing types or conveys compile-time information.

How 2 Manning books explain it

Write Powerful Rust Macros

A marker trait is a trait with no methods or associated properties that classifies implementing types.

How it is used
Enforces valid traffic-light transitions and required builder-field sequences at compile time.
How deep it goes
Works through marker traits, empty state structs, generic bounds, PhantomData, specialized implementations, generated builder states, and compile-fail testing.
Examples
Uses Green and Red traffic lights, a Gleipnir builder, generated field-state structs, a successful builder test, and a missing-property compile test.
What you can do after
Build it from scratch to encode state transitions and restrict build methods to completed builder states.

Read how Write Powerful Rust Macros explains marker trait →

Idiomatic Rust

A marker trait is a Rust trait that conveys compile-time information about a type without necessarily defining methods or other behavior.

How it is used
Names type attributes or a combination of trait bounds so generic code can require them through one marker.
How deep it goes
Works through an empty trait, blanket implementation, derived type, and generic bound; offers no single prescribed use case.
Examples
Uses a derived unit struct, an empty `FullFeatured` trait, its blanket implementation, and a generic container constrained by that trait.
What you can do after
Build it from scratch with an empty trait and blanket implementation, then use it as a generic bound.

Read how Idiomatic Rust explains marker trait →

Related concepts

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.