m04-zero-cost

Guide Rust developers in choosing generics versus trait objects for polymorphism.

3|Updated Mar 22, 2026
One-click install
npx skills add https://github.com/0xharryriddle/codex-field-kit --skill m04-zero-cost-0xharryriddle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/0xharryriddle/codex-field-kit/tree/main/archive/upstream/chasebuild-agent-skills/rust/skills/m04-zero-cost
Command: npx skills add https://github.com/0xharryriddle/codex-field-kit --skill m04-zero-cost-0xharryriddle

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps Rust developers decide between compile-time generics and runtime trait objects to implement polymorphism efficiently, avoiding unnecessary recompilation costs or dynamic dispatch overhead.

Core Features & Use Cases

  • Decision guidance for when to use generics vs trait objects based on type knowledge, heterogeneity, and performance priorities.
  • Practical mappings of common patterns (static dispatch, dynamic dispatch, impl Trait) with concise trade-offs.
  • Real-world scenario guidance for API design and library workflows.

Quick Start

Review the decision guide and examples to determine whether to use generics or trait objects for a given Rust polymorphism scenario.

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 versus trait objects for polymorphism?

Rust generics use monomorphization to generate specific code for each type, improving runtime speed but increasing binary size and recompilation costs, whereas dynamic dispatch uses vtables for smaller binaries at a slight runtime cost.

How does monomorphization affect compile-time performance in Rust?

Monomorphization in Rust generates distinct code for each concrete type used with generics, which maximizes runtime execution speed but significantly increases compile times and final binary size due to code duplication.

What is object safety and why does it matter for Rust trait objects?

Object safety in Rust determines whether a trait can be used as a trait object for dynamic dispatch, requiring that all methods have compatible signatures to allow valid runtime polymorphism via vtables.

Can I use impl Trait for dynamic dispatch in Rust APIs?

You can use impl Trait in Rust function arguments and return positions to simplify static dispatch generics, but it does not enable dynamic dispatch or heterogenous collections like boxed trait objects do.

What are the trade-offs between static dispatch and dynamic dispatch in Rust?

Static dispatch in Rust eliminates runtime overhead through compile-time monomorphization, while dynamic dispatch uses trait objects to provide API flexibility across diverse types at the cost of vtable lookup overhead.