m04-zero-cost

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

1|2|Updated Oct 10, 2025
One-click install
npx skills add https://github.com/dojoengine/torii-core --skill m04-zero-cost-dojoengine
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: m04-zero-cost
Source: https://github.com/dojoengine/torii-core/tree/main/.agents/skills/m04-zero-cost
Command: npx skills add https://github.com/dojoengine/torii-core --skill m04-zero-cost-dojoengine

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Rust developers often face trade-offs between compile-time generics and runtime dispatch, and this skill helps design zero-cost abstractions that preserve performance while maintaining expressiveness.

Core Features & Use Cases

  • Guided decision making: determine when to use generics vs trait objects to achieve zero-cost abstraction.
  • Performance awareness: analyze monomorphization costs and dynamic dispatch overhead for common Rust patterns.
  • Real-world scenarios: apply to library API design, performance-critical code paths, and generic abstractions.

Quick Start

Apply zero-cost abstraction guidelines to decide between generics and trait objects for a given Rust API or path.

Frequently Asked Questions about m04-zero-cost

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

FAQPage Schema
What is the best way to achieve zero-cost abstraction in Rust?▼

Zero-cost abstraction in Rust is achieved by deciding between compile-time generics and runtime trait objects to preserve performance while maintaining API expressiveness.

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

Choosing between static dispatch and dynamic dispatch involves analyzing monomorphization costs versus dynamic dispatch overhead for your specific performance-sensitive code paths.

When should I use trait objects instead of generics in Rust?▼

Use trait objects instead of generics when dynamic dispatch overhead is acceptable and you need runtime polymorphism rather than compile-time monomorphization guarantees.

Does using Rust generics increase compile times due to monomorphization?▼

Rust generics increase compile times due to monomorphization generating specialized code for each type, which trades compile overhead for zero-cost runtime performance.

Why does dynamic dispatch with trait objects impact performance in Rust?▼

Dynamic dispatch impacts performance because trait objects use a vtable pointer lookup at runtime instead of static dispatch, adding indirect call overhead to execution paths.

How to document design decisions for Rust generics and trait objects?▼

Document design decisions by validating compile-time guarantees for static dispatch and recording the trade-offs accepted when selecting dynamic trait objects for your library APIs.