rust-api-design
Skill32 starsChanged 4 months agoBSD-3-ClauseView on GitHub
Design a Rust public API — trait design, generics versus dyn, sealed traits, what is exported, and which changes break semver. Use when designing or reviewing a crate public surface, when choosing between a generic parameter and a trait object, when adding a trait method or enum variant to a released crate, or when the user asks whether a change is a breaking change.
7 other projects use a version of this.
Adopted by
Most recently taken up by
- adxptived/Rust-Skillstheir own version, first seen Oct 17, 2026
- takurot/rust-skills-comprehensivetheir own version, first seen Oct 15, 2026
- full-stack-skills/rust-skillstheir own version, first seen Oct 15, 2026
- Lu1sDV/skillsmdtheir own version, first seen Oct 13, 2026
- rcore-os/tgoskitstheir own version, first seen Oct 8, 2026
- jscott3201/yet-more-skillstheir own version, first seen Sep 7, 2026
Other versions7 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 7
- rcore-os/tgoskits★ 81same name
- full-stack-skills/rust-skills★ 8same name
- botirkhaltaev/botir-skills★ 4same name
- jscott3201/yet-more-skills★ 4same name
- Lu1sDV/skillsmd★ 2same name
- adxptived/Rust-Skills★ 1same name
- takurot/rust-skills-comprehensive★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository28 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- rust-ffiSkill
- rust-macrosSkill
- rust-serdeSkill
- rust-supply-chainSkill
- setup-rust-ciSkill
- setup-rust-pre-commitSkill
- port-from-cppSkill
- port-from-cSkill
- port-from-goSkill
- port-from-javaSkill
- port-from-pythonSkill
- port-from-typescriptSkill
- port-to-rustSkill
- async-rustSkill
- idiomatic-rustSkill
- ownership-not-cloneSkill
- rust-concurrencySkill
- rust-docsSkill
- rust-errorsSkill
- rust-observabilitySkill
- rust-performanceSkill
- type-driven-designSkill
- unsafe-rustSkill
- rust-code-reviewSkill
- rust-skills-mapSkill
- rust-testingSkill
- setup-rust-skillsSkill
The file
We split this skill into the rules it gives an agent
5 rules. 0 are common, 2 are distinctive, and 3 are very specific to this project.
0
Common
Most of the 6 edited versions we read keep these.
2
Distinctive
This author's own choices. Worth a look.
3
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
Treat these rules as requirements when writing a library, because callers you cannot see depend on its surface.
- Very specific: line 22
Default to private and export deliberately: a type pub only because a pub fn returns it is still part of the API, impls and public fields …
- Distinctive: line 91
Document that the trait is sealed, in the trait docs, so an implementer who hits the error knows it is deliberate.
- Very specific: line 147
Run clippy at the lint level configured in the repo — never force missing_docs or another lint on to make the surface check pass.
- Very specific: line 155
Do not install tooling into the user environment as a side effect.
“Common” is measured: kept by at least 6 in 10 of the 6 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Rust API Design
Everything pub from the crate root is a promise to every downstream caller. This
skill governs what callers can see and rely on — the exported surface, the shape
of its traits and generics, and which changes break the promise.
Libraries bind harder than applications
Treat these rules as requirements when writing a library, because callers you
cannot see depend on its surface. In an application every caller lives in your
own repository, so they are strong defaults that give way when simplicity is
worth more than reuse. An application may take a Vec, skip the derives nobody
uses, and stay on the concrete rung; a library has no such freedom.
The surface is the contract
Default to private and export deliberately: a type pub only because a pub fn
returns it is still part of the API, impls and public fields included. Make
pub(crate) the habit; re-export the surface from lib.rs, module paths private:
// lib.rs
pub use crate::internal::parser::Parser; // the surface
mod internal; // the tree stays privateParser is reachable by one path only: renaming the module, moving the file,
splitting the crate — none of it is a breaking change, because none of it is visible.
The surface refuses three shapes: an item public at two paths; an Arc, Rc,
Box or RefCell in a public signature; and a dependency type in a signature,
making that dependency part of the semver contract. The depth, including fields,
modules, crate size, parameter and return types, re-exports and #[must_use], is
in SURFACE.md; how a public type is built (constructors, Default, builders) is
in CONSTRUCTION.md.
Getting a dependency in
The ladder, lowest rung first: a concrete type — pass the thing; a wrapper struct —
swap the inside without touching a signature; a generic parameter — static dispatch,
but it infects every type that holds it; dyn Trait — the infection stops, at the
price of object safety. Climb only when the current rung cannot express the
requirement, never for a test-only need — ADR 0006 belongs to /rust-testing. The
cost of each rung is in DEPENDENCY-INJECTION.md.
Generics or dyn
A library prefers generics by default; reach for dyn when one of the rows below
applies, including a compile-time or code-size budget and code loaded at runtime.
| Want | Choose | Cost |
| Monomorphized speed, inlining, no vtable | impl Trait / <T: Trait> | Code bloat; every call site instantiates; one concrete type per parameter; verbose signatures |
| Heterogeneous collection, plugin registry, smaller binary, faster compiles, code loaded at runtime | dyn Trait | One copy plus a vtable dispatch; the trait must be object-safe |
| Argument position, caller convenience | impl Trait argument | The caller cannot turbofish |
| Return position in a public API | named type or impl Trait | impl Trait hides the type — a deliberate choice, not a shortcut |
Object safety usually settles it: generic methods, Self in arguments or returns
beyond the receiver, associated constants used through the object, and
associated functions that take no self (a fn new() -> Self constructor, a
fn name() -> &'static str helper) all rule out dyn; a heterogeneous registry cannot be generic without an enum or a trait
object. A method that would break object safety, a no-self associated function
included, can opt out with where Self: Sized, which removes it from the
vtable and keeps the rest of the trait usable as dyn. Return-position impl Trait is concise but awkward when callers
also need Debug or Clone bounds on the opaque type; it suits internal APIs
best.
Trait design
A trait is shared behaviour callers write generic code against, not a namespace
for grouping related functions. Keep the required methods small and default the
rest — every required method is a promise to every future implementer. A default
method may carry a bound of its own (where Self::Item: Clone), so it exists
only for implementers that satisfy it and costs the rest nothing; that is how
Iterator::cloned is offered to iterators over Clone items alone. Use an
associated type for the one sensible choice per implementer (Iterator::Item), a
generic parameter for several (From<T>). Watch blanket impls — impl<T: Foo> Bar
for T closes the door on any later impl Bar for SomeType that does not satisfy
Foo; the trap fires when the second impl is written, not when the blanket is.
Sealed traits
A sealed trait exists to let the crate add methods without breaking downstream implementers: the supertrait lives in a private module, so only this crate can implement it, though downstream code can still name the trait as a bound. Document that the trait is sealed, in the trait docs, so an implementer who hits the error knows it is deliberate. Readers reproduce the pattern wrong from memory, so here it is:
struct Widget;
mod sealed {
pub trait Sealed {}
}
pub trait WidgetBuilder: sealed::Sealed {
fn new() -> Self;
}
impl sealed::Sealed for Widget {}
impl WidgetBuilder for Widget {
fn new() -> Self {
Widget
}
}What breaks and what does not
The headline cases: adding a variant to a public enum breaks exhaustive matches
(unless the enum is #[non_exhaustive]); adding a required trait method breaks
implementers, a default body does not; adding a field to a public struct that is
not #[non_exhaustive] breaks literal construction; narrowing an argument type
breaks callers; widening a return type breaks callers who programmed against
the old one. The full table, including the non-obvious ones, the #[non_exhaustive]
exceptions, and the MSRV policy, is in SEMVER.md.
Standard traits, features, no_std, destructors
What a public type should implement and why callers cannot add it themselves, plus
destructor rules, is in STANDARD-TRAITS.md. Cargo features (additive only) and
making a library no_std are in FEATURES.md.
Deferrals
The shape of the error type this API returns is /rust-errors. Whether an argument
type should be a newtype carrying an invariant is /type-driven-design. Whether a
public function should be async, and what that commits the crate to, is
/async-rust. Whether a pub unsafe fn is justified and how its contract is
documented is /unsafe-rust.
Verification
cargo doc --no-deps # every public item documented and the links resolve
cargo clippy --all-targets # add -- -D warnings only if the repo has no lint config
cargo testRun clippy at the lint level configured in the repo — never force missing_docs or
another lint on to make the surface check pass; propose it instead. When the crate
is published or has downstream consumers, the semver check the agent runs and reads:
cargo semver-checks check-release # requires cargo-semver-checks; skip with a note if unavailableDo not install tooling into the user environment as a side effect. If
cargo-semver-checks is not present, say so and fall back to the SEMVER.md table.
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.

