type-safety

Newtype pattern for type-safe primitives

The newtype pattern wraps a primitive in a tuple struct to create a distinct type. This prevents mixing up values that are semantically different but have the same underlying type (like UserId(u32) vs PostId(u32)). The compiler enforces that you can't

Type state pattern for compile-time state machines

The typestate pattern uses Rust's type system to encode state machines, making invalid states unrepresentable. Each state is a separate type, and transitions consume self and return a new state. The compiler prevents calling methods that aren't valid