rust-type-driven

Enforce Rust domain invariants at compile time using type-driven design patterns.

44|7|Updated Jan 22, 2026
One-click install
npx skills add https://github.com/huiali/rust-skills --skill rust-type-driven
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: rust-type-driven
Source: https://github.com/huiali/rust-skills/tree/main/.codex/skills/rust-type-driven
Command: npx skills add https://github.com/huiali/rust-skills --skill rust-type-driven

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill helps Rust developers prevent runtime errors by encoding domain-specific rules and invariants directly into their types, making invalid states unrepresentable and ensuring compile-time safety.

Core Features & Use Cases

  • Newtype Pattern: Create distinct types for values with the same representation but different semantics (e.g., UserId vs. OrderId).
  • Type State Pattern: Encode state machines where transitions are enforced by the type system, preventing invalid operations.
  • Builder Pattern with Type State: Construct complex objects with compile-time guarantees for required fields.
  • PhantomData: Mark ownership and variance for zero-cost abstractions.
  • Zero-Sized Types (ZST): Use types with no runtime cost for compile-time markers and abstractions.
  • Use Case: Prevent accidental mixing of UserId and OrderId by using distinct UserId and OrderId newtypes, ensuring type safety at compile time.

Quick Start

Use the rust-type-driven skill to demonstrate the newtype pattern for user and order IDs in Rust.

Frequently Asked Questions about rust-type-driven

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent mixing different ID types like UserId and OrderId in Rust?▼

Using the newtype pattern prevents mixing UserId and OrderId in Rust by creating distinct types for values with the same representation but different semantics, ensuring type safety at compile time.

What is the type state pattern in Rust and how does it enforce compile-time safety?▼

The type state pattern in Rust encodes state machines where transitions are enforced by the type system, making invalid states unrepresentable and preventing invalid operations at compile time.

How do I build complex Rust objects with compile-time guarantees for required fields?▼

Build complex Rust objects with compile-time guarantees by applying the builder pattern with type state, which constructs objects ensuring all required fields are initialized before use.

When should I use PhantomData and zero-sized types in Rust?▼

Use PhantomData and zero-sized types (ZST) in Rust when you need to mark ownership and variance for zero-cost abstractions or use compile-time markers without incurring any runtime cost.

Does encoding domain invariants in Rust types eliminate runtime error classes?▼

Encoding domain invariants in Rust types eliminates runtime error classes by addressing type confusion, invalid state transitions, and uninitialized configurations through type-driven design patterns.

What is the best way to enforce domain invariants and make invalid states unrepresentable in Rust?▼

The best way to make invalid states unrepresentable in Rust is type-driven design, utilizing newtypes, marker traits, and builder patterns to enforce domain invariants directly within the type system.