agentleFS
gurinderugurinderu/craft/skills/rust-traits/SKILL.md

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.

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
  1. Very specific:

    associated types: when a trait has one natural output type per impl (Iterator::Item).

    line 37

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 Trait vs 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 type

Bounds 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:

ApproachSet of typesLayoutCostPick when
Generics / impl Traitone per call site (homogeneous)monomorphized, inlinablecode bloat, no runtime costthe type is known at compile time
dyn Trait (Box<dyn>, &dyn)open, heterogeneousvtable indirection, usually boxedalloc + no inliningopen extensibility, plugins, Vec of mixed types
enum dispatchclosed, heterogeneousenum on stack, matcha little boilerplatea 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

ErrorMeaningFix
E0277trait bound T: Trait not satisfiedadd the bound, or impl Trait for T
E0599method not foundbring the trait into scope (use), or a bound is missing
E0308mismatched typestwo impl Trait returns aren't the same type; box them or use an enum
E0038trait is not object-safe (not dyn-compatible)can't make dyn — use generics or enum dispatch (dispatch.md)
E0119conflicting/orphan implyou 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.

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.