rust-types

Design Rust polymorphic interfaces using generics and trait objects.

Updated Apr 21, 2026
One-click install
npx skills add https://github.com/gregoryraymond/claude-agents-and-skills --skill rust-types
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-types
Source: https://github.com/gregoryraymond/claude-agents-and-skills/tree/main/skills/rust-types
Command: npx skills add https://github.com/gregoryraymond/claude-agents-and-skills --skill rust-types

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Rust developers frequently face the trade-offs between static and dynamic dispatch, generic code readability, and safe API design. This skill helps you design polymorphic interfaces using generics, trait objects, and type-driven patterns (like Newtype, PhantomData, and type-state) to keep APIs safe, expressive, and efficient.

Core Features & Use Cases

  • Explains static vs dynamic dispatch with concrete examples and when to choose each approach.
  • Covers Newtype wrappers, PhantomData, type-state, marker/sealed traits, and builder patterns to encode invariants in the type system.
  • Guides API design for library boundaries and performance-critical paths, balancing ergonomics with compile-time guarantees.

Quick Start

Define a newtype wrapper around a primitive, implement a trait, and demonstrate a type-state lifecycle to enforce valid state transitions.

Frequently Asked Questions about rust-types

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

FAQPage Schema
How do I choose between static and dynamic dispatch in Rust?

Static dispatch uses generics and monomorphization for zero-cost performance, while dynamic dispatch uses trait objects for runtime flexibility. Choose generics for performance-critical code and trait objects when you need heterogeneous collections or runtime polymorphism at API boundaries.

What is the type-state pattern in Rust and how does it enforce valid state transitions?

The type-state pattern encodes state transitions into the type system, making invalid states unrepresentable at compile time. It uses PhantomData and marker traits to ensure objects follow a valid lifecycle, guaranteeing safety without runtime checks.

When should I use PhantomData in Rust generic structs?

Use PhantomData in Rust generic structs when you need to indicate ownership or variance relationships without storing actual data. It allows you to track type parameters for type-safety abstractions and implement traits that require specific type associations.

What is the best way to design polymorphic interfaces in Rust?

Design polymorphic Rust interfaces by combining generics for static dispatch, trait objects for dynamic dispatch, and type-driven patterns like sealed traits. This balances API ergonomics with compile-time guarantees for library boundaries.

How do I use sealed traits to restrict trait implementations in Rust?

Sealed traits prevent external crates from implementing your traits by using a private internal module marker. This ensures type-safe abstractions and maintains API stability by restricting trait implementation to types defined within your library.

Does using impl Trait in Rust generics affect performance compared to dyn Trait?

Using impl Trait enables static dispatch through monomorphization, offering better performance than dyn Trait. However, dyn Trait provides dynamic dispatch flexibility for heterogeneous collections, creating a trade-off between compile-time speed and runtime polymorphism.