---
title: Rust Best Practices Skill for Claude Code
canonical: https://skillselion.com/skills/apollographql/skills/rust-best-practices
content_type: text/markdown
description: Rust Best Practices is a skill for writing idiomatic Rust following Apollo GraphQL conventions.
---

# Rust Best Practices Skill

Title: Rust Best Practices Skill for Claude Code

> Rust Best Practices is a skill for writing idiomatic Rust following Apollo GraphQL conventions.

Canonical: https://skillselion.com/skills/apollographql/skills/rust-best-practices

Rust Best Practices is a skill for writing idiomatic Rust following Apollo GraphQL conventions.

Rust Best Practices is a skill covering idiomatic Rust coding patterns with emphasis on error handling, borrowing discipline, and performance. Developers reference it when writing new Rust code or reviewing existing implementations. Addresses the challenge of writing efficient, maintainable Rust.

## Key facts

- Installs: 23,255
- GitHub stars: 131
- Author: apollographql
- Repository: apollographql/skills
- Works in Grok Bot: SKILL.md frontmatter name `rust-best-practices` and a description are valid. The instructions do not depend on Claude Code hooks, slash commands, the Task or subagent tool, or the Claude CLI. Checked 2026-11-17.
- Install command: `npx skills add https://github.com/apollographql/skills --skill rust-best-practices`
- Last updated: November 6, 2026

## What Rust Best Practices does

Rust Best Practices is a skill covering idiomatic Rust coding patterns with emphasis on error handling, borrowing discipline, and performance. Developers reference it when writing new Rust code or re

When should Rust code borrow instead of cloning?

Idiomatic Rust patches favoring borrows, justified Clone usage, and loop-safe ownership patterns in reviewed code.

Best for: Rust developers writing backend services, CLI tools, or GraphQL servers

Not for: Greenfield Rust learners needing a full language tutorial before writing their first function.

- Idiomatic Rust patterns following Apollo conventions
- Covers error handling, ownership, and performance optimization
- Includes testing strategies and documentation practices

## SKILL.md

## 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

## Rust Best Practices FAQ

### When does rust-best-practices recommend cloning?

rust-best-practices recommends Clone when preserving an immutable original while mutating a copy, when using Arc or Rc shared pointers, when APIs require owned data, or for cached snapshots - not as the default inside hot loops.

### What is the core idioms focus in rust-best-practices?

rust-best-practices Chapter 1 emphasizes borrowing over cloning, documents Clone traps such as implicit clones in loop iterators, and gives pragmatic exceptions for non-performance-critical refactors.

### Is Rust Best Practices safe to install?

skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.

## Machine access

- [Sitemap](https://skillselion.com/sitemap.xml)
- [llms.txt](https://skillselion.com/llms.txt)
- [llms-full.txt](https://skillselion.com/llms-full.txt)
- [Developer resources](https://skillselion.com/developers)
- [OpenAPI spec](https://skillselion.com/openapi.json)
- [MCP handshake](https://skillselion.com/.well-known/mcp)
- [API catalog](https://skillselion.com/.well-known/api-catalog)
- [AI catalog](https://skillselion.com/.well-known/ai-catalog.json)
- [Pricing](https://skillselion.com/pricing.md)
- [Auth](https://skillselion.com/auth.md)
- [AGENTS.md](https://skillselion.com/AGENTS.md)
- [Contact](https://skillselion.com/contact)
- [Home](https://skillselion.com/)
