Choose generics or trait objects by asking when the type is known, with error-driven design checks.

skills.sh installs
3.7K
GitHub stars
1.6K
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers the static-versus-dynamic dispatch decision for generics, impl Trait, and dyn Trait, with trade-offs for each
  • Maps errors E0277, E0308, E0599, and E0038 to design questions rather than quick fixes
  • Includes a quick-reference table of dispatch, code size, and runtime cost, plus an object-safety checklist
  • Designed for Rust developers debugging trait bound errors or deciding how to abstract over types

Install

$ npx -y skills add actionbook/rust-skills --skill m04-zero-cost -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Zero-Cost Abstraction

Layer 1: Language Mechanics

Core Question

Do we need compile-time or runtime polymorphism?

Before choosing between generics and trait objects:

  • Is the type known at compile time?
  • Is a heterogeneous collection needed?
  • What's the performance priority?

Error → Design Question

ErrorDon't Just SayAsk Instead
E0277"Add trait bound"Is this abstraction at the right level?
E0308"Fix the type"Should types be unified or distinct?
E0599"Import the trait"Is the trait the right abstraction?
E0038"Make object-safe"Do we really need dynamic dispatch?

Thinking Prompt

Before adding trait bounds:

  1. What abstraction is needed?

    • Same behavior, different types → trait
    • Different behavior, same type → enum
    • No abstraction needed → concrete type
  2. When is type known?

    • Compile time → generics (static dispatch)
    • Runtime → trait objects (dynamic dispatch)
  3. What's the trade-off priority?

    • Performance → generics
    • Compile time → trait objects
    • Flexibility → depends

Trace Up ↑

Details

Repository
github.com/actionbook/rust-skills
Category
Rust
First seen
May 16, 2026

More from actionbook/rust-skills

All 38

Similar to M04 Zero Cost

More Rust