actionbook avatar

M05 Type Driven

Pushes you to encode invariants in the type system before reaching for runtime validation. When you're about to add an is_valid flag or validate in every function, this asks whether the compiler could catch it instead. Covers newtypes for primitive obsession, type state patterns for state machines, PhantomData for variance markers, and builder patterns for gradual construction. The "Error to Design Question" table is the real gem here, reframing common runtime checks as type design opportunities. Honestly most useful when you catch yourself writing the same validation logic in multiple places. Traces up to domain modeling when unclear what types to create, traces down to concrete patterns like sealed traits and zero-sized types.

skills.sh installs
3.6K
GitHub stars
1.6K
last updated
Aug 21, 2026

Install

$ npx -y skills add actionbook/rust-skills --skill m05-type-driven --agent claude-code

Installs into .claude/skills of the current project.

Files

Type-Driven Design

Layer 1: Language Mechanics

Core Question

How can the type system prevent invalid states?

Before reaching for runtime checks:

  • Can the compiler catch this error?
  • Can invalid states be unrepresentable?
  • Can the type encode the invariant?

Error → Design Question

PatternDon't Just SayAsk Instead
Primitive obsession"It's just a string"What does this value represent?
Boolean flags"Add an is_valid flag"Can states be types?
Optional everywhere"Check for None"Is absence really possible?
Validation at runtime"Return Err if invalid"Can we validate at construction?

Thinking Prompt

Before adding runtime validation:

  1. Can the type encode the constraint?

    • Numeric range → bounded types or newtypes
    • Valid states → type state pattern
    • Semantic meaning → newtype
  2. When is validation possible?

    • At construction → validated newtype
    • At state transition → type state
    • Only at runtime → Result with clear error

Sponsors

Details

Repository
github.com/actionbook/rust-skills
Categories
Rust, Design & UI/UX
First seen
May 16, 2026

More from actionbook/rust-skills

All 38

Recommended

More Rust
1actionbook avatarm12-lifecycleactionbook/rust-skillsUse when designing resource lifecycles. Keywords: RAII, Drop, resource lifecycle, connection pool, lazy initialization, connection pool design, resource cleanup3.5Kskills.sh installs
2actionbook avatardomain-cliactionbook/rust-skillsUse when building CLI tools. Keywords: CLI, command line, terminal, clap, structopt, argument parsing, subcommand, interactive, TUI, ratatui, crossterm, indicat3.4Kskills.sh installs
3actionbook avatardomain-mlactionbook/rust-skillsUse when building ML/AI apps in Rust. Keywords: machine learning, ML, AI, tensor, model, inference, neural network, deep learning, training, prediction, ndarray3Kskills.sh installs
4actionbook avatarcore-actionbookactionbook/rust-skillsInternal support skill for actionbook MCP selectors used by Rust documentation research workflows. Use only when another rust-skills workflow explicitly request2Kskills.sh installs
5actionbook avatarcore-fix-skill-docsactionbook/rust-skillsInternal maintenance support for checking and fixing generated Rust skill documentation references. Use only when explicitly invoked by /fix-skill-docs.2Kskills.sh installs
6zhanghandong avatarcoding-guidelineszhanghandong/rust-skillscoding guidelines1.2Kskills.sh installs