m04-zero-cost

Guide Rust library design between generics and trait objects for zero-cost abstractions.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/mberetvas/dbt-migrator --skill m04-zero-cost-mberetvas
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/mberetvas/dbt-migrator/tree/main/.github/skills/m04-zero-cost
Command: npx skills add https://github.com/mberetvas/dbt-migrator --skill m04-zero-cost-mberetvas

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Conserves performance and clarity by guiding when to use generics or trait objects to achieve zero-cost abstractions in Rust.

Core Features & Use Cases

  • Guidance on when to choose generics vs trait objects
  • Examples of monomorphization and dynamic dispatch in Rust
  • Use cases across library design and API stability

Quick Start

Ask me to analyze a Rust API scenario and select generics or dyn Trait to achieve zero-cost abstraction.

Frequently Asked Questions about m04-zero-cost

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

FAQPage Schema
When should I use generics vs trait objects in Rust for zero-cost abstractions?

Choose generics for static dispatch and maximum performance when type sets are known at compile time, and trait objects for dynamic dispatch when flexibility across varying types is required. This guidance optimizes library design by balancing monomorphization trade-offs against runtime flexibility.

How does monomorphization affect Rust performance with generics?

Monomorphization generates specialized code for each generic type at compile time, enabling static dispatch and zero-cost abstractions while increasing binary size. This Skill analyzes your API scenarios to guide when these compile-time trade-offs benefit performance-critical code paths.

What is the difference between impl Trait and dyn Trait in Rust?

impl Trait uses static dispatch for zero-cost abstractions via compile-time monomorphization, whereas dyn Trait uses dynamic dispatch allowing runtime polymorphism. This Skill guides the selection between them based on your library API stability and performance requirements.

How do I handle object safety when designing Rust library APIs?

Object safety determines whether a trait can be used as a dyn Trait object for dynamic dispatch. This Skill provides practical guidelines for addressing object safety requirements and selecting between generics or trait objects in your Rust library design.

Can I use zero-cost abstractions across modules with varying type sets in Rust?

Yes, by analyzing usage patterns across modules to select generics for static dispatch or trait objects for dynamic dispatch. This Skill evaluates scenarios with varying type sets to maintain zero-cost abstractions and optimize performance-critical code paths.

Why does my Rust generic code increase binary size significantly?

Generics cause monomorphization, duplicating code for each type to achieve zero-cost static dispatch. This Skill analyzes your Rust API to guide trade-offs between monomorphization binary bloat and dynamic dispatch flexibility for performance optimization.