m04-zero-cost

Guide Rust generics versus trait objects for zero-cost polymorphism decisions.

Updated Mar 22, 2026
One-click install
npx skills add https://github.com/cecon123/tg-remote-bot --skill m04-zero-cost-cecon123
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/cecon123/tg-remote-bot/tree/main/.agents/skills/m04-zero-cost
Command: npx skills add https://github.com/cecon123/tg-remote-bot --skill m04-zero-cost-cecon123

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Decisions around polymorphism in Rust—choosing between generics (static dispatch) and trait objects (dynamic dispatch)—to achieve zero-cost abstractions and optimal performance.

Core Features & Use Cases

  • Guidance on when to use generics vs trait objects for libraries and applications
  • Explanations of object safety, monomorphization, and common error patterns (e.g., E0277, E0308)
  • Practical design considerations for reusable APIs and plugin architectures

Quick Start

Ask the AI to explain how to choose between generics and trait objects for a zero-cost Rust API.

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

Choose generics for static dispatch and monomorphization when performance is critical, and trait objects for dynamic dispatch when runtime flexibility is needed. This skill guides the Rust polymorphism decision process by evaluating monomorphization trade-offs, object safety constraints, and API design patterns.

What is object safety in Rust and how does it affect trait object creation?

Object safety in Rust determines whether a trait can be used as a trait object for dynamic dispatch. This skill explains object safety rules and common error patterns like E0277 and E0308, showing how methods lacking certain properties prevent trait object creation.

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

Static dispatch via generics inlines code for performance, while dynamic dispatch via trait objects enables runtime polymorphism. This skill provides practical design considerations for reusable Rust APIs, weighing code reuse, performance, and binary size.

Why does monomorphization increase binary size in Rust and how can I manage it?

Monomorphization in Rust duplicates code for each generic type instantiation, increasing binary size. This skill explains monomorphization trade-offs and provides guidelines for balancing static dispatch performance against binary size constraints.

Can I use dynamic dispatch for plugin architectures in Rust without losing zero-cost abstractions?

Dynamic dispatch via trait objects enables plugin-like architectures in Rust, though it introduces runtime overhead compared to generics. This skill provides design considerations for plugin architectures while maintaining optimal performance where possible.

What are common Rust compiler errors when implementing trait objects and generics?

Common Rust compiler errors when implementing polymorphism include E0277 and E0308, often related to type mismatches or object safety violations. This skill explains these error patterns and provides practical guidelines to resolve them.