m04-zero-cost

Decide between generics and trait objects for zero-cost abstraction in Rust.

Updated Feb 8, 2026
One-click install
npx skills add https://github.com/yumazak/kodo --skill m04-zero-cost-yumazak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/yumazak/kodo/tree/main/.agents/skills/m04-zero-cost
Command: npx skills add https://github.com/yumazak/kodo --skill m04-zero-cost-yumazak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Rust developers often face a tradeoff between zero-cost generics and dynamic dispatch via trait objects. This skill helps decide when to apply generics or trait objects to achieve zero-cost abstractions while preserving flexibility and safety.

Core Features & Use Cases

  • Provides criteria for choosing static dispatch (monomorphization) vs dynamic dispatch (trait objects)
  • Illustrates the performance and code-size implications of each approach
  • Offers practical guidance for common patterns like impl Trait, dyn Trait, and explicit trait bounds in Rust

Quick Start

Provide a recommended decision for a Rust function or type to use generics or trait objects and explain the expected performance and code-size impact.

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 Rust generics vs trait objects for zero-cost abstractions?

Choose Rust generics for static dispatch when you know types at compile time to achieve zero-cost abstractions, and use trait objects for dynamic dispatch when runtime polymorphism is required for flexibility.

How does monomorphization affect code size in Rust?

Monomorphization in Rust generates specific machine code for each generic type instantiation, maximizing runtime performance through static dispatch but increasing binary code size due to duplicated function implementations.

Can I use impl Trait for dynamic dispatch in Rust?

No, impl Trait in Rust primarily enables static dispatch at compile time. For dynamic dispatch, you must explicitly use dyn Trait to create trait objects and leverage runtime polymorphism.

What are the object safety requirements for Rust trait objects?

Rust trait objects require the trait to be object-safe, meaning it cannot have methods returning Self or taking generic parameters, ensuring dynamic dispatch works correctly at runtime via vtables.

How do I decide between static and dynamic dispatch for a Rust library?

For a Rust library, use static dispatch with explicit trait bounds to maximize performance, or offer dynamic dispatch via trait objects to reduce compile times and binary size for diverse consumers.

Why does using generics cause longer compile times than trait objects?

Using generics causes longer Rust compile times because monomorphization forces the compiler to generate and optimize separate static dispatch code for every concrete type used, unlike dynamic dispatch.