rust-api-design
Skill0 starsChanged 3 months agoApache-2.0View on GitHub
Design predictable Rust public APIs — method naming conventions, doc comment structure, and which std traits to implement. Use when writing a library crate's public surface, reviewing API ergonomics, naming a constructor/conversion/accessor method, or deciding whether a type needs Debug/Display/Eq/Ord/Hash/Clone/Copy/From/TryFrom.
6 other projects use a version of this.
Adopted by
Most recently taken up by
- adxptived/Rust-Skillstheir own version, first seen Oct 17, 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
- botirkhaltaev/botir-skillstheir own version, first seen Jul 24, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- rcore-os/tgoskits★ 76same 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
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 repository11 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- rust-asyncSkill
- rust-concurrency-syncSkill
- rust-ffiSkill
- rust-newtype-and-raiiSkill
- rust-ownership-and-lifetimesSkill
- rust-pinningSkill
- rust-polymorphismSkill
- rust-typestate-and-tokensSkill
- rust-unsafe-fundamentalsSkill
- rust-unsafe-soundnessSkill
The file
We split this skill into the rules it gives an agent
4 rules. 0 are common, 4 are distinctive, and 0 are very specific to this project.
0
Common
Most of the 5 edited versions we read keep these.
4
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 39
If implementation details genuinely matter to the caller, it's because of an effect or invariant they need to know about (e.g.
- Distinctive: line 119
Default to only what the type's actual usage needs — implementing a trait is a promise about behavior, not a checkbox.
- Distinctive: line 140
Write the doc comment as *contract*: what's guaranteed, what errors mean, link out to unfamiliar terms — not how it's implemented or where …
- Distinctive: line 146
If it's library code with many callers, invest in doc quality.
“Common” is measured: kept by at least 6 in 10 of the 5 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
For general idiom defaults, see rust-patterns first. This skill is for the specific job of
designing or reviewing a public API surface: what to name things, what to document, and
which standard traits a type should implement.
Doc comments: what, not how or where
Doc comments describe the contract a caller can rely on — not the implementation, and not where the API is currently used. Both leak details that change without the doc being updated.
// Bad — documents implementation, which is irrelevant and goes stale fast
/// Saves a `User` record to the Postgres database.
///
/// This function opens a new connection and begins a transaction. It checks
/// if a user with the given ID exists with a `SELECT` query...
pub fn save_user(user: &User) -> Result<(), db::Error> { ... }
// Good — documents the guarantee
/// Atomically saves a user record.
///
/// # Errors
///
/// Returns `db::Error::DuplicateUsername` if the user (keyed by
/// `user.username`) already exists.
pub fn save_user(user: &User) -> Result<(), db::Error> { ... }If implementation details genuinely matter to the caller, it's because of an effect or invariant they need to know about (e.g. "blocks the calling thread," "not safe for concurrent calls") — document that, not the mechanism producing it.
Avoid redundancy — the item's name and signature already are documentation. Don't restate them:
// Redundant — the signature already says this
/// Parses an ipv4 from a str. Returns an option for failure modes.
fn parse_ip_addr_v4(input: &str) -> Option<IpAddrV4> { ... }
// Redundant — the field name already says this
struct BusinessAsset {
/// The customer id.
customer_id: u64,
}
// Don't lead with the type/function's own name either
/// `ServerSynchronizer` is an orchestrator that sends local edits...
struct ServerSynchronizer { ... }
// Better: state the purpose directly
/// Sends local edits to the server and reconciles conflicts.
struct ServerSynchronizer { ... }Rule of thumb: ask "what does the reader still not know after reading the name and type
signature?" — document only that gap. Don't explain Result, the ? operator, or other
language/stdlib basics; assume an intermediate Rust reader.
Name-drop and signpost, don't over-explain. Readers skim; put the load-bearing keyword
near the start of a sentence so scanning finds it, and link out ([term][anchor]) to let an
unfamiliar reader self-serve rather than inlining a tutorial:
/// A parsed representation of a [MARC 21 record leader][leader].
///
/// [leader]: https://www.loc.gov/marc/bibliographic/bdleader.html
pub struct Leader { ... }Library vs. application docs are different jobs. Library code has many users, a broad
problem scope, and a stable API — it can afford elaborate docs, including some restated
signature info, because the ROI is high. Application code has few users, a narrow purpose,
and churns often — keep its docs terse; heavy doc-coverage lints (#![warn(missing_docs)])
are appropriate for library crates, not generally for application code.
Naming conventions
Rust doesn't have a new keyword — naming is the API's grammar. Getting these conventions
right is what makes an API guessable without reading docs.
| Prefix/suffix | Meaning | Example |
new | The canonical constructor | Vec::new(), Box::new(x) |
with_capacity, with_* (constructor) | Constructor that sets one specific parameter, defaults elsewhere | Vec::with_capacity(n) |
with_* (copy-and-set) | Non-consuming: copy self, change one thing, return new value — "like self but with X different" | path.with_extension("mov") |
with(closure) | Takes a closure to configure something before consuming/returning | builder-style config methods |
is_* | Boolean predicate about the value | Vec::is_empty(), f32::is_nan() |
[method]_mut | Same access as [method] but returns &mut | get / get_mut, iter / iter_mut |
try_* | Fallible version, returns Result with a specific error type | TryFrom::try_from, Receiver::try_recv |
from | Constructor implying a type conversion — prefer over hand-rolled into (see below) | Duration::from_days(n) |
into_* | Consumes self, returns another owned type | IntoIterator::into_iter, Box<str>::into_string |
to_* | Non-consuming conversion, borrows self and returns an owned value (or takes self by value if Copy) | str::to_owned(), str::to_uppercase() |
as_* / _ref | Cheap reference-to-reference reinterpretation, no allocation | Rc::as_ref, Option::as_ref, Option::as_slice |
by | Method takes a custom projection/comparator closure | slice::sort_by, sort_by_key |
From/Into — prefer implementing From<T> on your type, not Into<Yours> for T.
A From impl gives you the corresponding Into for free (blanket impl), but not the reverse.
For function parameters, prefer impl Into<String> as the bound over where String: From<T>
— it reads more clearly at the call site as "accepts anything convertible into a String."
Same rule for the fallible pair: implement TryFrom<T>, not TryInto.
Getting these names wrong has a real cost: an into_foo that borrows instead of
consuming, or a to_foo that's actually expensive/allocating on a type callers expect to be
free, breaks the implicit contract these prefixes carry and will surprise every caller who's
used the standard library.
Choosing which traits to implement
Default to only what the type's actual usage needs — implementing a trait is a promise about behavior, not a checkbox.
| Trait | Derivable? | Implement when… | Watch out for |
Debug | ✅ | Almost always — needed for {:?}, dbg!, assertions in tests. | — |
Display | ❌ (hand-write) | The type has a genuine user-facing string form. | Don't implement just to reuse {} in logs — that's what Debug/{:?} is for. |
PartialEq | ✅ | Value has a meaningful equality notion. | "Partial" means some values may not compare as expected (e.g. f32::NAN != f32::NAN) — not that it panics. |
Eq | ✅ | Equality is total — every value equals itself, no NaN-like exceptions. Requires PartialEq. | Can't manually diverge: deriving both keeps them consistent. |
PartialOrd/Ord | ✅ | Values have a meaningful order, e.g. for sorting or BTreeMap keys. | Derived order follows declaration order — struct fields top-to-bottom, enum variants top-to-bottom. Ord requires Eq + PartialOrd; keep both consistent. |
Hash | ✅ | Type is used as a HashMap/HashSet key. Requires Eq. | Hash and Eq must agree: if a == b then hash(a) == hash(b) — don't hand-write one without the other, or derive both. |
Clone | ✅ | Duplicating the value (or bumping an Rc/Arc refcount) is a valid operation. | Skip it if duplicating would violate an invariant the type exists to enforce (unique handles, single-owner resources — see rust-newtype-and-raii). |
Copy | ✅ | Type is small "plain data" that should behave like a primitive (numeric wrappers, small enums). | Requires Clone; always derive both together, never hand-write Clone alongside a derived Copy — a custom clone() could diverge from what an implicit bitwise copy does. Impossible on types with Drop or non-Copy fields — that's a hint the type is not "plain data." |
From<T> | ❌ | An infallible, non-panicking conversion from T exists. | Implement From, not Into (see above). |
TryFrom<T> | ❌ | The conversion from T can fail. | Give it a specific, meaningful Error type — not () or a generic string. |
Serialize/Deserialize (serde) | via #[derive(Serialize, Deserialize)] | Type crosses a serialization boundary (config, wire format, storage). | Changing field names/order is a wire-format break once this is derived — treat it as part of the public contract. |
Quick checklist for a new public item
- Name it using the table above — if it converts, copies, checks a condition, or borrows mutably, there's almost certainly a convention that already fits.
- Write the doc comment as contract: what's guaranteed, what errors mean, link out to unfamiliar terms — not how it's implemented or where it's called from.
- Derive
Debugunless there's a specific reason not to (e.g. it would leak secrets — hand- write a redactingDebuginstead). - Walk the traits table and implement only what the type's real usage requires; don't derive
Clone/Copyreflexively on a type meant to represent a unique resource or handle. - If it's library code with many callers, invest in doc quality; if it's application code, keep docs terse and expect them to churn with the code.
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.

