rust-best-practices
This skill guides agents in writing idiomatic Rust code following Apollo GraphQL's best practices handbook.
Identity proven by a skills index on apollographql.com
- Agents
- Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode, claude.ai and API
- License
- MIT (permissive)
- Pin
- 5f02fcfpinned Oct 9, 2026
- Context cost
- about 930 tokenswhen the skill loads
- GitHub stars
- 117on apollographql/skills
- Scripts
- None found
What it does
“Guide for writing idiomatic Rust code based on Apollo GraphQL's best practices handbook. Use this skill when: (1) writing new Rust code or functions, (2) reviewing or refactoring existing Rust code, (3) deciding between borrowing vs cloning or ownership patterns, (4) implementing error handling with Result types, (5) optimizing Rust code for performance, (6) writing tests or documentation for Rust projects.”
Generated summary from the skill's own text
This skill guides agents in writing idiomatic Rust code following Apollo GraphQL's best practices handbook. It covers ownership patterns, error handling, performance optimization, testing strategies, and documentation standards across nine reference chapters. The agent can apply these guidelines when writing, reviewing, or refactoring Rust code and running build tools like cargo, rustfmt, and clippy.
When to use it
- Writing new Rust functions or modules from scratch
- Reviewing or refactoring existing Rust code for improvements
- Deciding between borrowing, cloning, or ownership transfer patterns
- Implementing error handling with Result types in Rust code
When not to
- Working with non-Rust programming languages
- Rust versions below 1.70 or incompatible with Cargo
- When quick prototyping is prioritized over code quality
Install
These commands install from the publisher's repository and need no account.
skills CLI
npx skills add apollographql/skills --skill rust-best-practicesInstalls from the repository's default branch, which may have changed since the commit we checked.
GitHub CLI 2.90 or later
gh skill install apollographql/skills rust-best-practices@5f02fcfcebec678e2e55777b639e32346622a963Installs the commit we checked (5f02fcf).
Copy by hand
Download the folder at 5f02fcf and put it in .claude/skills/rust-best-practices for Claude Code and in .agents/skills/rust-best-practices for Codex, Cursor, Gemini CLI, GitHub Copilot and OpenCode.
Claude Code plugin
/plugin install apollo-skills@claude-plugins-officialThe publisher's plugin in Claude's official marketplace. It may hold more than this skill, and we have not pinned it.
Copy for your agent
A prompt for any coding agent. It installs this skill from GitHub at the pinned commit, copies the files as they are, and runs nothing unless you ask.
Prompt
Install the agent skill "rust-best-practices" by Apollo GraphQL. Source, pinned to commit 5f02fcfcebec678e2e55777b639e32346622a963: https://github.com/apollographql/skills/tree/5f02fcfcebec678e2e55777b639e32346622a963/skills/rust-best-practices 1. Download that folder at exactly this commit. Do not use the default branch. 2. Copy every file unchanged, keeping the folder structure, into .claude/skills/rust-best-practices/ for Claude Code and .agents/skills/rust-best-practices/ for Codex, Cursor, Gemini CLI, GitHub Copilot and OpenCode. Create the folders if they do not exist. 3. Do not edit the files, and do not run any script in the folder unless I ask you to. 4. When you are done, list the files you copied and where you put them. Listing and checks: https://www.agentnative.dev/skills/apollo-graphql/rust-best-practices
SKILL.md at the pin
---
name: rust-best-practices
description: >
Guide for writing idiomatic Rust code based on Apollo GraphQL's best practices handbook. Use this skill when:
(1) writing new Rust code or functions,
(2) reviewing or refactoring existing Rust code,
(3) deciding between borrowing vs cloning or ownership patterns,
(4) implementing error handling with Result types,
(5) optimizing Rust code for performance,
(6) writing tests or documentation for Rust projects.
license: MIT
compatibility: Rust 1.70+, Cargo
metadata:
author: apollographql
version: "1.1.2"
allowed-tools: Bash(cargo:*) Bash(rustc:*) Bash(rustfmt:*) Bash(clippy:*) Read Write Edit Glob Grep
---
# Rust Best Practices
Apply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's [Rust Best Practices Handbook](https://github.com/apollographql/rust-best-practices).
## 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](references/chapter_01.md): Borrowing vs cloning, Copy trait, Option/Result handling, iterators, comments, when to extract a function (duplication vs. wrong abstraction)
- [Chapter 2 - Clippy and Linting](references/chapter_02.md): Clippy configuration, important lints, workspace lint setup
- [Chapter 3 - Performance Mindset](references/chapter_03.md): Profiling, avoiding redundant clones, stack vs heap, zero-cost abstractions
- [Chapter 4 - Error Handling](references/chapter_04.md): Result vs panic, thiserror vs anyhow, error hierarchies
- [Chapter 5 - Automated Testing](references/chapter_05.md): Test naming, one assertion per test, snapshot testing
- [Chapter 6 - Generics and Dispatch](references/chapter_06.md): Static vs dynamic dispatch, trait objects
- [Chapter 7 - Type State Pattern](references/chapter_07.md): Compile-time state safety, when to use it
- [Chapter 8 - Comments vs Documentation](references/chapter_08.md): When to comment, doc comments, rustdoc
- [Chapter 9 - Understanding Pointers](references/chapter_09.md): 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:
```rust
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
From apollographql/skills at 5f02fcf, shown as plain text under its license: MIT (permissive). The license text is in skills/rust-best-practices/SKILL.md.
View SKILL.md on GitHub at 5f02fcfFiles
- SKILL.md4.4 KB · b1651a95a2c3
- references/
- chapter_01.md26 KB · def0d5dc6ad2
- chapter_02.md5.5 KB · 3b5d4ad26ce4
- chapter_03.md8.8 KB · fd673405cc1b
- chapter_04.md7.1 KB · 5189a1118c41
- chapter_05.md15 KB · fb753a7515e1
- chapter_06.md7.0 KB · cff0a6826a23
- chapter_07.md7.9 KB · be71105ee912
- chapter_08.md11 KB · 54c1548a2858
- chapter_09.md11 KB · bdd59af111d4
10 files, 103 KB in total. Each file has a sha256 in the pin's manifest; the first 12 characters are shown.
Skill folder on GitHub at 5f02fcfMore from Apollo GraphQL
apollo-client
This skill guides building React applications with Apollo Client 4.x for managing GraphQL queries, mutations, and state.
Claude Code, Codex, Cursor +4 · about 1.7K tokens · pinned 6 days ago
apollo-connectors
This skill helps integrate REST APIs into GraphQL supergraphs using Apollo Connectors with @source and @connect directives.
Claude Code, Codex, Cursor +4 · about 1.6K tokens · pinned 6 days ago
apollo-federation
This skill guides agents in authoring Apollo Federation subgraph schemas using Federation 2.x directives like @key, @shareable, @external, and @requires.
Claude Code, Codex, Cursor +4 · about 830 tokens · pinned 6 days ago
apollo-ios
This skill guides building GraphQL-powered Apple apps with Apollo iOS, a strongly-typed Swift client.
Contains scripts
Claude Code · about 1.8K tokens · pinned 6 days ago
apollo-kotlin
This skill guides building GraphQL applications with Apollo Kotlin, a strongly typed client library for Android, JVM, and Kotlin Multiplatform projects.
Contains scripts
Claude Code, Codex, Cursor +3 · about 370 tokens · pinned 6 days ago
apollo-router
Generates version-correct Apollo Router configuration (v1.x or v2.x) for federated GraphQL supergraphs.
Claude Code, Codex, Cursor +4 · about 5.1K tokens · pinned 6 days ago
Related in Coding workflow
shopify
This skill enables agents to work across Shopify's entire developer platform, including Admin API, Storefront GraphQL, Shopify Functions, CLI operations, and…
by ShopifyOfficial publisher
Contains scripts
Claude Code · about 3.9K tokens · pinned 6 days ago
graphql-operations
This skill guides developers in writing efficient, type-safe GraphQL operations including queries, mutations, and subscriptions.
by Apollo GraphQLOfficial publisher
Claude Code, Codex, Cursor +4 · about 1.2K tokens · pinned 6 days ago
practice-registry
This skill checks Scala, Python, TypeScript, JavaScript, and Rust code against company-specific coding practices before generation, during editing, and before…
by WriterOfficial publisher
Claude Code, Codex, Cursor +4 · about 410 tokens · pinned 6 days ago
tauri-v2
Develops cross-platform desktop and mobile applications using Tauri v2, with a Rust backend and web frontend.
by nodnarbnitramCommunity
Claude Code, Codex, Cursor +3 · about 4K tokens · pinned 6 days ago
planning
This skill orchestrates multi-workflow projects with dependencies and shared data tables by creating a task graph in the orchestrator via the create-tasks tool.
by n8nOfficial publisher
Claude Code, Codex, Cursor +3 · about 2.4K tokens · pinned 6 days ago
progressive-building
This skill implements incremental workflow development for agents building or extending workflows.
by n8nOfficial publisher
Claude Code, Codex, Cursor +3 · about 1.4K tokens · pinned 6 days ago