agentleFS
abipalliabipalli/agent-skills/skills/rust-best-practices/SKILL.md

rust-best-practices

Skill1 starsChanged 18 days agoApache-2.0View on GitHub

Idiomatic Rust guidance (ownership, errors, performance, tests, docs), adapted from Apollo GraphQL's handbook. Use when writing, refactoring or reviewing Rust.

57 other projects use a version of this, 36 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use rust-port, rust-async-patterns and 9 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • fedemeloSkill

    rust-port

    Port code from C, C++, Python, Go, JavaScript, or TypeScript into idiomatic Rust that keeps the original behavior: ownership instead of manual memory, Result instead of error codes and exceptions, enums instead of flag ints and magic strings, and tests that prove the port matches. Use this whenever the user asks to port, translate, convert, migrate, or rewrite code in Rust ("rewrite this in Rust", "what would this look like in Rust", "move this module to Rust"), even if they don't say "port". Pairs with the rust-best-practices skill, which covers the target code's style.

    Also in 5 projects that use this

  • klauswgSkill

    rust-async-patterns

    Master Rust async programming with Tokio, async traits, error handling, and concurrent patterns. Use when building async Rust applications, implementing concurrent systems, or debugging async code.

    Also in 11 projects that use this

  • GitHubSkill

    rust-mcp-server-generator

    Generate a complete Rust Model Context Protocol server project with tools, prompts, resources, and tests using the official rmcp SDK

    Also in 3 projects that use this

  • huialiSkill

    rust-testing

    Also in 9 projects that use this

  • gihwan-devSkill

    tauri-v2

    Tauri v2 cross-platform app development with Rust backend. Use when configuring tauri.conf.json, implementing Rust commands (#[tauri::command]), setting up IPC patterns (invoke, emit, channels), configuring permissions/capabilities, troubleshooting build issues, or deploying desktop/mobile apps. Triggers on Tauri, src-tauri, invoke, emit, capabilities.json.

    Also in 7 projects that use this

  • xfstudioSkill

    inngest

    Inngest expert for serverless-first background jobs, event-driven workflows, and durable execution without managing queues or workers. Use when: inngest, serverless background job, event-driven workflow, step function, durable execution.

    Also in 3 projects that use this

The file

Show its rules and text

We split this skill into the rules it gives an agent

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

  • 2

    Common

    Most of the 56 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 15

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Common:

    Before reviewing, familiarize yourself with Apollo's Rust best practices.

    line 19
  2. Very specific:

    Prefer &T over .clone() unless ownership transfer is required

    line 34
  3. Very specific:

    Use &str over String, &[T] over Vec<T> in function parameters

    line 35
  4. Very specific:

    Use Cow<'_, T> when ownership is ambiguous

    line 37
  5. Very specific:

    Never use unwrap()/expect() outside tests

    line 41
  6. Very specific:

    Use thiserror for library errors, anyhow for binaries only

    line 42
  7. Very specific:

    Prefer ? operator over match chains for error propagation

    line 43
  8. Very specific:

    Always benchmark with --release flag

    line 46
  9. Very specific:

    Run cargo clippy -- -D clippy::perf for performance hints

    line 47
  10. Very specific:

    Avoid cloning in loops; use .iter() instead of .into_iter() for Copy types

    line 48
  11. Very specific:

    Prefer iterators over manual loops.

    line 49
  12. Very specific:

    Use #[expect(clippy::lint)] over #[allow(...)] with justification comment.

    line 59
  13. Very specific:

    Use doc tests (///) for public API examples

    line 64
  14. Very specific:

    Consider cargo insta for snapshot testing generated output

    line 65
  15. Common:

    Prefer generics (static dispatch) for performance-critical code

    line 68
  16. Very specific:

    Use dyn Trait only when heterogeneous collections are needed

    line 69
  17. Very specific:

    Every TODO needs a linked issue: // TODO(#42): ...

    line 87

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

Apply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's Rust Best Practices Handbook, licensed Apache-2.0; the chapters in references/ are adapted from it.

Prerequisites: a Rust toolchain (1.70+) with Cargo, Clippy and rustfmt.

Best Practices Reference

Before reviewing, familiarize yourself with Apollo's Rust best practices. Read ALL relevant chapters in the same turn in parallel. Reference these files when providing feedback:

  • Chapter 1 - Coding Styles and Idioms: Borrowing vs cloning, Copy trait, Option/Result handling, iterators, comments
  • Chapter 2 - Clippy and Linting: Clippy configuration, important lints, workspace lint setup
  • Chapter 3 - Performance Mindset: Profiling, avoiding redundant clones, stack vs heap, zero-cost abstractions
  • Chapter 4 - Error Handling: Result vs panic, thiserror vs anyhow, error hierarchies
  • Chapter 5 - Automated Testing: Test naming, one assertion per test, snapshot testing
  • Chapter 6 - Generics and Dispatch: Static vs dynamic dispatch, trait objects
  • Chapter 7 - Type State Pattern: Compile-time state safety, when to use it
  • Chapter 8 - Comments vs Documentation: When to comment, doc comments, rustdoc
  • Chapter 9 - Understanding Pointers: Thread safety, Send/Sync, pointer types

Quick Reference

Borrowing & Ownership

  • Prefer &T over .clone() unless ownership transfer is required
  • Use &str over String, &[T] over Vec<T> in function parameters
  • Small Copy types (≤24 bytes) can be passed by value
  • Use Cow<'_, T> when ownership is ambiguous

Error Handling

  • Return Result<T, E> for fallible operations; avoid panic! in production
  • Never use unwrap()/expect() outside tests
  • Use thiserror for library errors, anyhow for binaries only
  • Prefer ? operator over match chains for error propagation

Performance

  • Always benchmark with --release flag
  • Run cargo clippy -- -D clippy::perf for performance hints
  • Avoid cloning in loops; use .iter() instead of .into_iter() for Copy types
  • Prefer iterators over manual loops; avoid intermediate .collect() calls

Linting

Run regularly: cargo clippy --all-targets --all-features --locked -- -D warnings

Key lints to watch:

  • redundant_clone - unnecessary cloning
  • large_enum_variant - oversized variants (consider boxing)
  • needless_collect - premature collection

Use #[expect(clippy::lint)] over #[allow(...)] with justification comment.

Testing

  • Name tests descriptively: process_should_return_error_when_input_empty()
  • One assertion per test when possible
  • Use doc tests (///) for public API examples
  • Consider cargo insta for snapshot testing generated output

Generics & Dispatch

  • Prefer generics (static dispatch) for performance-critical code
  • Use dyn Trait only when heterogeneous collections are needed
  • Box at API boundaries, not internally

Type State Pattern

Encode valid states in the type system to catch invalid operations at compile time:

struct Connection<State> { /* ... */ _state: PhantomData<State> }
struct Disconnected;
struct Connected;

impl Connection<Connected> {
    fn send(&self, data: &[u8]) { /* only connected can send */ }
}

Documentation

  • // comments explain why (safety, workarounds, design rationale)
  • /// doc comments explain what and how for public APIs
  • Every TODO needs a linked issue: // TODO(#42): ...
  • Enable #![deny(missing_docs)] for libraries

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.