m04-zero-cost

Select Rust static or dynamic dispatch abstractions for code scenarios.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill helps Rust developers decide whether to use generic static dispatch or trait‑object dynamic dispatch, preventing performance pitfalls and compile‑time errors caused by mismatched abstractions.

Core Features & Use Cases

  • Decision Guide: Asks key questions to determine the right abstraction based on compile‑time knowledge and performance priorities.
  • Error Mapping: Links common compiler errors (E0277, E0308, E0599, E0038) to design questions.
  • Trade‑off Analysis: Provides quick comparison of static vs dynamic dispatch, enum vs trait objects, and when to prefer each.

Quick Start

Use the skill to decide whether to apply generics or trait objects for a specific Rust code challenge.

Frequently Asked Questions about m04-zero-cost

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

FAQPage Schema
How do I choose between static dispatch and dynamic dispatch in Rust?

Choose static dispatch via generics when types are known at compile time and maximum performance is required; use trait objects for dynamic dispatch when runtime polymorphism is needed, accepting a slight performance cost.

What is the difference between Rust generics and trait objects for performance?

Generics enable zero-cost abstractions through compile-time monomorphization, while trait objects introduce runtime vtable lookups, making generics faster and trait objects more flexible for heterogeneous types.

Why do I get Rust compiler error E0277 when using trait objects?

Rust compiler error E0277 occurs when a type does not implement a required trait, often happening when mixing static dispatch generics with trait boundaries that are not satisfied by the concrete type.

When should I avoid using generics in favor of trait objects?

Avoid generics and use trait objects when you need to store different types in a single collection or reduce compile times, as dynamic dispatch handles runtime polymorphism without creating multiple monomorphized copies.

How do I fix Rust error E0038 about object safety?

Fix Rust error E0038 by removing methods with generic parameters or Self types from the trait definition, ensuring the trait is object safe so it can be used as a trait object for dynamic dispatch.

Can I use enums instead of trait objects for polymorphism in Rust?

Yes, you can use enums instead of trait objects for polymorphism in Rust when the set of possible types is fixed, providing static dispatch performance without the vtable overhead of dynamic dispatch.