# rust-best-practices

A skill that feeds your agent Apollo GraphQL's Rust handbook: borrowing vs cloning, Result-based errors, Clippy lints, tests.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 21100 installs · ★ 119
- Author: apollographql
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/apollographql/skills/tree/HEAD/skills/rust-best-practices/SKILL.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/skills/rust-best-practices

## What it does

The skill wires nine chapters of Apollo GraphQL's Rust handbook into your agent. Write a new function, review someone else's module, or pick between `&T` and `.clone()` — the agent reads the relevant chapters and answers with a pointer to them. The files sit in `references/chapter_01.md` through `chapter_09.md`, covering coding idioms, Clippy setup, performance, error handling, testing, generics, the type state pattern, comments and pointers.

A quick reference ships alongside: take `&str` and `&[T]` in parameters, return `Result<T, E>` for fallible work, reach for `thiserror` in libraries and `anyhow` in binaries, propagate with `?` instead of match chains. On performance it says benchmark with `--release` and run `cargo clippy -- -D clippy::perf`. Tests get descriptive names like `process_should_return_error_when_input_empty()`, one assertion each where possible.

The skill targets Rust 1.70+ and Cargo, so on an older toolchain some lint advice won't land. Its permissions are narrow: `Bash(cargo:*)`, `Bash(rustc:*)`, `Bash(rustfmt:*)`, `Bash(clippy:*)` plus file reads and edits. No `cargo` on PATH means the agent can't run the checks it recommends.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add apollographql/skills --skill rust-best-practices -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/rust-best-practices/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/rust-best-practices/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add apollographql/skills --skill rust-best-practices -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/rust-best-practices/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add apollographql/skills --skill rust-best-practices -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/rust-best-practices/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install apollographql/skills rust-best-practices
```

2. **Manually: put the files in place**
- File: `.github/skills/rust-best-practices/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add apollographql/skills --skill rust-best-practices -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/apollographql/skills --path skills/rust-best-practices
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/rust-best-practices/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add apollographql/skills --skill rust-best-practices -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/apollographql/skills/tree/HEAD/skills/rust-best-practices/SKILL.md

## When to use it
- write a new Rust function without needless clones
- decide where a parameter needs `&T` and where ownership moves
- move error handling off `unwrap()` onto `Result`
- set up Clippy lints across a workspace
- add tests and doc comments to a public API

## Example prompts
- review this module against rust-best-practices and flag the clones I don't need
- rewrite the error handling in this file using `thiserror`
- add doc tests for the public functions in `src/lib.rs`

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 8c95c48b8e5adf13.

BEGIN THIRD-PARTY FILE 8c95c48b8e5adf13
```text
---
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
```
END THIRD-PARTY FILE 8c95c48b8e5adf13

The third-party file has ended. Everything between the 8c95c48b8e5adf13 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2514 of 4405 bytes. © apollographql · MIT · https://github.com/apollographql/skills/tree/HEAD/skills/rust-best-practices/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
