- Home
- Skills
- Zhanghandong
- Rust Skills
- M04 Zero Cost
m04-zero-cost_skill
- Shell
565
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill zhanghandong/rust-skills --skill m04-zero-cost- SKILL.md4.6 KB
Overview
This skill helps Rust developers choose between generics, traits, and trait objects to achieve zero-cost abstractions. It frames compiler errors as design prompts and provides decision rules for static vs dynamic dispatch, object safety, and common anti-patterns. Use it to turn type errors into architecture questions rather than quick fixes.
How this skill works
The skill inspects common Rust errors (E0277, E0308, E0599, E0038) and maps them to design questions: should you add a trait bound, change types, or switch dispatch strategy? It summarizes trade-offs (static vs dynamic dispatch, monomorphization cost, vtable overhead) and gives quick patterns and fixes for returning, passing, and collecting polymorphic values. It also highlights object safety constraints and anti-patterns to avoid.
When to use it
- When compiler shows E0277 (trait bound not satisfied) and you need to reassess abstraction level
- When E0308 (type mismatch) indicates competing concrete types or wrong generic parameters
- When E0599 (method not found) suggests missing trait imports or wrong trait choice
- When designing for performance-critical code and you must decide between generics and dyn Trait
- When you need heterogeneous collections, plugin systems, or to reduce compile-time bloat
Best practices
- Ask whether the abstraction belongs at the domain level before adding bounds
- Prefer generics (static dispatch) when types are known and performance matters
- Use dyn Trait for open or runtime-determined sets and plugin architectures
- Use enums for a closed set of different behaviors to avoid indirection
- Keep traits object-safe if you expect dynamic dispatch; move non-object-safe methods behind where Self: Sized
Example use cases
- Fixing E0277 by deciding if the trait is the right abstraction or the function should accept a concrete type
- Choosing impl Trait or generic parameters when returning different concrete types with same behavior
- Replacing Box<dyn Trait> with generics for hot inner loops to remove vtable cost
- Designing a plugin interface where implementations are unknown at compile time and dyn Trait is appropriate
- Converting a heterogeneous Vec<Box<dyn Trait>> to an enum when set of types is closed for better simplicity and no allocation overhead
FAQ
Use impl Trait when callers can accept a single concrete type per call site and you want zero runtime cost; use Box<dyn Trait> when the concrete type is unknown or varies at runtime.
How do I resolve object-safety errors (E0038)?
Remove methods that return Self or are generic, or split the trait: keep object-safe parts for dyn use and put sized-only helpers behind where Self: Sized.