agentleFS
leroyguillaumeleroyguillaume/claude/rust-conventions/SKILL.md

rust-conventions

Skill7 starsChanged 3 days agoMITView on GitHub

Rust projects: crate choices (clap, tokio, tracing, mockall) and layout. TRIGGER when: editing `.rs` files; touching `Cargo.toml`, `Cargo.lock` or `.cargo/config.toml`; adding a Rust dependency; setting up a Rust CLI, server, async trait, mock or subscriber; a `cargo run`/`cargo test` needing env vars. SKIP when: no Rust is involved. Load `rust-http-conventions` alongside for HTTP, `rust-build-conventions` for the toolchain pin, sqlx tests, `build.rs`, cargo-chef and coverage.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

17 rules. 0 are common, 0 are distinctive, and 17 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 17

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Prefer static dispatch over dynamic dispatch.

    line 18
  2. Very specific:

    Prefer argument-position impl Trait over a named generic parameter when the parameter's type is used only once and never needs to be named.

    line 23
  3. Very specific:

    Keep the named generic only when the type must actually be referred to: it is used in more than one place, you need it via turbofish …

    line 27
  4. Very specific:

    Prefer default_value_t = <typed value> over a stringly-typed default_value = "..." whenever the field's type is anything other than a …

    line 50
  5. Very specific:

    Use tokio as the async runtime.

    line 98
  6. Very specific:

    Always set up structured logging/tracing with tracing and tracing-subscriber.

    line 122
  7. Very specific:

    Add local cargo fmt --check and cargo clippy hooks to .pre-commit-config.yaml.

    line 182
  8. Very specific:

    Use mockall for test doubles.

    line 225
  9. Very specific:

    Never default to Box<dyn Trait> / &dyn Trait when generics or impl Trait express the same thing with static dispatch.

    line 244
  10. Very specific:

    Never use an async runtime other than tokio (async-std, smol, …).

    line 246
  11. Very specific:

    Never set force = true in a .cargo/config.toml [env] table, and never put a credential there that reaches anything beyond the project's own …

    line 247
  12. Very specific:

    Never front a documented cargo run / cargo test with a wall of VAR=value assignments when .cargo/config.toml can carry them.

    line 250
  13. Very specific:

    Never use println! / eprintln!

    line 252
  14. Very specific:

    Never add a clap option without an env = "..." attribute, and never build the EnvFilter from EnvFilter::from_env / from_default_env or a …

    line 254
  15. Very specific:

    Never leave clippy warnings unaddressed.

    line 258
  16. Very specific:

    Never hand-roll mock structs, fakes, or stubs for a trait when mockall can generate them, and never pull in another mocking crate (mockers, …

    line 260
  17. Very specific:

    Never reach for the async-trait crate.

    line 263

“Common” is measured: kept by at least 6 in 10 of the 4 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.

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

Rust conventions

Always:

  • Prefer static dispatch over dynamic dispatch. Use generics with trait bounds or impl Trait rather than Box<dyn Trait> / &dyn Trait. Reach for trait objects only when static dispatch is genuinely impossible (e.g. heterogeneous collections, recursive types, or a hard binary-size constraint), and leave a one-line comment explaining why.
  • Prefer argument-position impl Trait over a named generic parameter when the parameter's type is used only once and never needs to be named. Write fn run(handler: impl Handler), not fn run<H: Handler>(handler: H) / fn run<H>(handler: H) where H: Handler. Keep the named generic only when the type must actually be referred to: it is used in more than one place, you need it via turbofish (foo::<H>()), you reference its associated types (H::Item), it appears in the return type, or the function is a trait-impl method whose signature must match the trait declaration. The named form is strictly more verbose for the single-use case, so default to impl Trait there.
  • Lay out modules with the <module>.rs + <module>/ directory form, not <module>/mod.rs. A module that has children lives in foo.rs alongside a foo/ directory holding its submodules (foo/bar.rs), never in foo/mod.rs. The mod.rs style is discouraged: it scatters many identically named files across the tree and makes editor tabs ambiguous. An existing mod.rs layout is migrated only on request or as a change of its own, never as a drive-by inside unrelated work.
  • In a Cargo workspace, put each member crate in its own directory at the repository root (<repo>/<crate-name>/), not nested under a crates/ (or packages/, libs/, …) subdirectory. List members explicitly in the root [workspace] members = [...] rather than globbing a wrapper dir. The root Cargo.toml is a virtual manifest ([workspace] only, no [package]). An existing crates/-style layout is flattened only on request or as a change of its own.
  • Build CLIs with clap (derive API), giving every argument an env = "..." attribute. Precedence and variable names follow config-conventions.
  • Prefer default_value_t = <typed value> over a stringly-typed default_value = "..." whenever the field's type is anything other than a String/&str — enums, integers, paths, durations, etc. default_value_t takes a real value of the field's type (compile-time checked, rendered via Display), so the default cannot drift out of sync with the type and a typo is a build error rather than a runtime parse failure. (A plain String flag may keep default_value = "info".) An enum deriving clap::ValueEnum and marked value_enum needs nothing more: #[arg(long, env = "...", value_enum, default_value_t = Mode::Auto)]. Any other custom type used as a default needs a Display impl mirroring its FromStr.
  • Give the development defaults a .cargo/config.toml with an [env] table, committed at the repository root, as soon as a binary needs an environment variable to start. cargo run and cargo test then work from a fresh clone with nothing exported, and the DATABASE_URL=… JWT_SECRET=… S3_BUCKET=… cargo run incantation disappears from CONTRIBUTING.md instead of being copy-pasted wrong.
    # .cargo/config.toml
    # Cargo sets these only for what it spawns (`cargo run`, `cargo test`), not
    # for ./target/debug/app run by hand or in a container.
    [env]
    DATABASE_URL = "postgres://app:app@localhost:5432/app"
    # Resolved against this file's parent directory, not the shell's cwd.
    MIGRATIONS_DIR = { value = "migrations", relative = true }
    Five rules keep it from becoming a liability:
    • Never force = true. Without it, a variable already present in the environment wins and the file stays a fallback; with it, CI's own DATABASE_URL and a container's real configuration are silently replaced by a development default. That single word is the difference between a convenience and a footgun.
    • Never a value the project's config file sets. [env] reaches the binary as environment variables, which beat the config file (config-conventions): a default here silently overrides what a contributor wrote there.
    • Only values that unlock the project's own throwaway stack — the docker-compose services and nothing else. A credential that reaches a shared or production system never goes in a committed file, whatever the variable is called.
    • Say in a comment that Cargo applies [env] only to processes it spawns. cargo run and cargo test get the values; running ./target/debug/app by hand, or the binary in a container, gets nothing. That is the first surprise, and it is cheaper to answer in the file than in a bug report.
    • Keep it the one home for those values. CONTRIBUTING.md says the file exists and that the environment takes precedence; it does not restate the values.
  • Use tokio as the async runtime.
  • In a long-running process, apply signal-handling-conventions. Rust mechanics: enable tokio's signal feature and write one shutdown_signal() future that completes on SIGTERM or SIGINT, then drive shutdown from it:
    async fn shutdown_signal() {
        use tokio::signal::unix::{signal, SignalKind};
        let mut interrupt = signal(SignalKind::interrupt()).expect("SIGINT handler");
        let mut terminate = signal(SignalKind::terminate()).expect("SIGTERM handler");
        tokio::select! { _ = interrupt.recv() => {}, _ = terminate.recv() => {} }
    }
    Handle both via SignalKind (don't mix ctrl_c() in for SIGINT — keep the two consistent). The above is unix-only, which is the right default for a Linux-container target; only if the binary must also run on Windows, guard it with #[cfg(unix)] and fall back to tokio::signal::ctrl_c() under #[cfg(not(unix))]. A server hands it to its graceful-shutdown hook (see rust-http-conventions); a worker loop races it with tokio::select!. tokio::signal::ctrl_c alone is not enough — it only covers SIGINT, so a container stopped with SIGTERM would never drain.
  • Any HTTP surface — framework, OpenAPI, input validation with validator — follows rust-http-conventions; load it before touching a handler, a router or a request/response DTO.
  • Always set up structured logging/tracing with tracing and tracing-subscriber. Initialise the subscriber once at the start of main, configured with an EnvFilter. The filter directive comes from a clap option (--log-filter, its variable named per config-conventions), never read directly from the environment — which also rules out RUST_LOG, read by other crates' own from_default_env() machinery. Document the directive syntax in the option's help so --help is self-sufficient. The log format is a clap option too (logging-conventions). tracing_subscriber::fmt() writes to stdout by default, which mixes logs into the program's result, so always point it at stderr. Example:
    use clap::Parser;
    
    #[derive(Clone, Copy, clap::ValueEnum)]
    enum LogFormat { Text, Json }
    
    #[derive(clap::Parser)]
    struct Cli {
        /// `tracing` filter directive (e.g. `info`, `myapp=debug,axum=warn`)
        ///
        /// Syntax: <https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives>
        #[arg(long = "log-filter", env = "MYTOOL_LOG_FILTER", default_value = "info")]
        log_filter: String,
        #[arg(long, env = "MYTOOL_LOG_FORMAT", value_enum,
              default_value_t = LogFormat::Text)]
        log_format: LogFormat,
    }
    
    fn main() -> Result<(), tracing_subscriber::filter::ParseError> {
        let cli = Cli::parse();
        // `EnvFilter::new` silently drops a directive it cannot parse.
        let fmt = tracing_subscriber::fmt()
            .with_env_filter(tracing_subscriber::EnvFilter::try_new(&cli.log_filter)?)
            .with_writer(std::io::stderr);
        match cli.log_format {
            LogFormat::Text => fmt.init(),
            LogFormat::Json => fmt.json().init(), // needs the `json` feature
        }
        Ok(())
    }
  • Apply logging-conventions. Rust mechanics: debug! (and trace! for very high-volume detail) via tracing, level controlled by the clap-parsed filter above, structured fields (debug!(%name, count, "…")) — never string interpolation, never println! / eprintln! for diagnostics.
  • Only the program's result goes to stdout; everything else goes to stderr. println! is for what the program produces — the thing a caller would pipe into a file or another command. Error messages, usage hints and notes to whoever is watching go to eprintln!, so that cmd > out.txt keeps the output clean and the diagnostics visible. The split is by audience, not by severity: stdout is for the next program in the pipeline, stderr is for the person at the terminal.

    This does not loosen the logging rule. The line is between what a CLI says to its user — results on stdout, a final error message on stderr before it exits non-zero — and diagnostics about the program's own workings, which go through tracing (writing to stderr, as above), never through either macro.

  • Format with rustfmt and lint with clippy (cargo clippy -- -D warnings). Add local cargo fmt --check and cargo clippy hooks to .pre-commit-config.yaml.
  • Declare lints once, in the manifest — never as #![warn(...)] attributes scattered across crate roots. In a workspace, the table lives in the root virtual manifest and every member opts in with [lints] workspace = true:
    # Cargo.toml (workspace root)
    [workspace.lints.rust]
    unsafe_code = "forbid"
    
    [workspace.lints.clippy]
    all = { level = "warn", priority = -1 }
    pedantic = { level = "warn", priority = -1 }
    # <crate>/Cargo.toml
    [lints]
    workspace = true
    Lint groups must carry priority = -1. Cargo turns each entry into a rustc flag and rustc applies "last flag wins", but Cargo ignores the order the entries are written in and sorts by priority instead. A group left at the default priority of 0 can therefore be emitted after an individual allow of one of its lints and silently re-enable it. clippy::all includes lint_groups_priority, which catches this and suggests the fix — do not ignore that warning.
  • No missing_docs. It demands a /// on every public item, which ends up paraphrasing signatures. When pedantic asks for # Errors or # Panics, write only what the signature cannot say: which input fails, and how.
  • Aim for an empty allow-list. pedantic is worth keeping on: its noisy lints still force a decision, and its cast/float lints catch real bugs long after the code was written. When a lint fires, the default is to comply. Blanket allow entries in the manifest are a last resort and must carry a comment explaining what was traded away — a global allow hides a problem everywhere, whereas a scoped #[allow(clippy::…)] at the offending item, with a justification comment, documents it exactly where it matters. Prefer the scoped form every time. Beware of writing the allow-list before the code: allows added at bootstrap "because everyone disables those" are cargo-cult, and some will not even be in the group any more.
  • Toolchain pin (in mise.toml), sqlx database tests, build.rs for sqlx::migrate!, cargo-chef Docker builds and test coverage live in rust-build-conventions; load it alongside for those.
  • Use mockall for test doubles. Define collaborators as traits, annotate them with #[cfg_attr(test, mockall::automock)] (or mock! when you cannot own the trait), and inject the mock in unit tests. Keep production injection on static dispatch (generics / impl Trait); only the test wiring of a synchronous trait may fall back to a trait object if generics make the seam impractical (async traits are not object-safe, see below). Add mockall under [dev-dependencies] via cargo add --dev mockall. mockall supports native async traits (Rust ≥ 1.75), so there is no need for async-trait to make a trait mockable — keep #[cfg_attr(test, mockall::automock)] above the trait definition.
  • For async methods on a trait, use a native async fn or, when the returned future must be Send (e.g. it crosses a tokio::spawn or a controller runtime), the desugared form fn f(&self, …) -> impl Future<Output = …> + Send. Such traits are not object-safe, which is fine: inject them with generics (T: Trait), not dyn Trait, consistent with the static-dispatch rule above.

Never:

  • Never default to Box<dyn Trait> / &dyn Trait when generics or impl Trait express the same thing with static dispatch.
  • Never use an async runtime other than tokio (async-std, smol, …).
  • Never set force = true in a .cargo/config.toml [env] table, and never put a credential there that reaches anything beyond the project's own development stack. Development defaults must lose to a real environment.
  • Never front a documented cargo run / cargo test with a wall of VAR=value assignments when .cargo/config.toml can carry them.
  • Never use println! / eprintln! (or ad-hoc log setup) for application diagnostics; route everything through tracing.
  • Never add a clap option without an env = "..." attribute, and never build the EnvFilter from EnvFilter::from_env / from_default_env or a direct std::env read — the directive must flow through the clap-parsed value.
  • Never leave clippy warnings unaddressed. If a lint must be allowed, add a scoped #[allow(...)] with a justification comment.
  • Never hand-roll mock structs, fakes, or stubs for a trait when mockall can generate them, and never pull in another mocking crate (mockers, faux, …) when mockall fits.
  • Never reach for the async-trait crate. Use native async fn / -> impl Future<Output = …> (+ Send) in traits and generic injection instead. The only exception is an external trait you do not own that is itself defined with #[async_trait].

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.