rust-traits
Skill1 starsChanged 40 days agoMITView on GitHub
Rust traits, generics, and dispatch — trait bounds, impl Trait, static vs dynamic vs enum dispatch, object safety, and type-driven design (newtype, typestate, PhantomData, sealed traits). Use when designing an abstraction, choosing generics vs dyn vs enum, fixing trait-bound errors, or encoding invariants in the type system. Triggers: impl Trait, dyn, enum dispatch, object safety, newtype, typestate, PhantomData, E0277, E0038, E0119.
In this repository42 other files it gives its agents.
- AGENTS.mdAGENTS.md
- addressing-findingsSkill
- auditSkill
- codebase-onboardingSkill
- codex-reviewSkill
- compatibilitySkill
- debuggingSkill
- developSkill
- distributed-racesSkill
- memorySkill
- nix-dev-envSkill
- nix-flakesSkill
- nixosSkill
- nix-packagingSkill
- nix-reviewSkill
- refactoringSkill
- retroSkill
- review-initSkill
- review-rulesSkill
- rust-architecture-reviewSkill
- rust-architectureSkill
- rust-cliSkill
- rust-cloud-nativeSkill
- rust-concurrencySkill
- rust-ecosystemSkill
- rust-embeddedSkill
- rust-errorsSkill
- rust-fintechSkill
- rust-idiomsSkill
- rust-macrosSkill
- rust-mlSkill
- rust-navigationSkill
- rust-ownershipSkill
- rust-performanceSkill
- rust-pluginsSkill
- rust-reviewSkill
- rust-securitySkill
- rust-testingSkill
- rust-unsafeSkill
- rust-webSkill
- specsSkill
- sql-reviewSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 37
associated types: when a trait has one natural output type per impl (Iterator::Item).
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Rust Traits & Generics
Traits define shared behavior; how you dispatch to it (static, dynamic, or enum) is the decision that shapes performance and extensibility. Type-driven design then uses traits and generics to make illegal states unrepresentable. Deep dives in the sub-files.
When to Use
- Designing an abstraction over multiple types
- Choosing generics vs
dyn Traitvs enum dispatch - Fixing trait-bound / "method not found" / object-safety errors
- Encoding invariants in types (newtype, typestate, sealed traits)
Quick model
trait Area { fn area(&self) -> f64; } // shared behavior
fn total<T: Area>(items: &[T]) -> f64 { // generic: monomorphized per T
items.iter().map(Area::area).sum()
}
fn describe(s: &impl Area) -> String { // impl Trait in arg = generic sugar
format!("area {}", s.area())
}
fn make() -> impl Area { Circle { r: 1.0 } }// impl Trait in return = one opaque concrete typeBounds go inline (<T: Area + Clone>) or in a where clause when they get long. Prefer
associated types when a trait has one natural output type per impl (Iterator::Item);
use generic parameters when a type can implement the trait many ways (From<T>).
The dispatch decision
The central choice when you have several types behind one trait:
| Approach | Set of types | Layout | Cost | Pick when |
Generics / impl Trait | one per call site (homogeneous) | monomorphized, inlinable | code bloat, no runtime cost | the type is known at compile time |
dyn Trait (Box<dyn>, &dyn) | open, heterogeneous | vtable indirection, usually boxed | alloc + no inlining | open extensibility, plugins, Vec of mixed types |
| enum dispatch | closed, heterogeneous | enum on stack, match | a little boilerplate | a known, fixed set; hot path; Vec without Box |
Full treatment with code in dispatch.md — including enum dispatch (and the
enum_dispatch crate) as the static, allocation-free middle ground between generics and dyn.
Coherence (the orphan rule)
You may impl Trait for Type only if you own the trait or the type. To implement a foreign
trait on a foreign type, wrap it in a newtype (see type-driven.md). This
is why E0119 / "only traits defined in the current crate" appears.
Error → fix
| Error | Meaning | Fix |
| E0277 | trait bound T: Trait not satisfied | add the bound, or impl Trait for T |
| E0599 | method not found | bring the trait into scope (use), or a bound is missing |
| E0308 | mismatched types | two impl Trait returns aren't the same type; box them or use an enum |
| E0038 | trait is not object-safe (not dyn-compatible) | can't make dyn — use generics or enum dispatch (dispatch.md) |
| E0119 | conflicting/orphan impl | you don't own trait or type — use a newtype |
Boundaries
- Lifetimes in bounds and references are mechanics of
rust-ownership; this skill is the trait and generic system. - The catalog of "idiomatic vs not" trait usage →
rust-idioms. - Using traits as ports to invert dependencies across an application →
rust-architecture.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

