{"data":{"slug":"discovered-60c200e5c75c10f3986ff10fc49bb2c0c8724a7ae480a376","title":"Rust Best Practices","summary":"Apply idiomatic Rust coding practices for writing, reviewing, and refactoring Rust code, including borrowing vs cloning, error handling with Result types, performance optimization, and linting with Clippy.","url":"https:\/\/skillshunter.net\/skills\/discovered-60c200e5c75c10f3986ff10fc49bb2c0c8724a7ae480a376","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-60c200e5c75c10f3986ff10fc49bb2c0c8724a7ae480a376","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-60c200e5c75c10f3986ff10fc49bb2c0c8724a7ae480a376.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-60c200e5c75c10f3986ff10fc49bb2c0c8724a7ae480a376\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before using this skill, familiarize yourself with Apollo's Rust best practices handbook, including all relevant chapters on coding styles, error handling, performance, and linting. Ensure that your project is compatible with Rust 1.70+ and Cargo.","instructions":"---\nallowed-tools: Bash(cargo:*) Bash(rustc:*) Bash(rustfmt:*) Bash(clippy:*) Read Write Edit Glob Grep\ncompatibility: Rust 1.70+, Cargo\ndescription: |\n    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.\nlicense: MIT\nmetadata:\n    author: apollographql\n    github-path: skills\/rust-best-practices\n    github-pinned: 9dda1397730370aeb8df4ff8f2015c34c86b5003\n    github-ref: 9dda1397730370aeb8df4ff8f2015c34c86b5003\n    github-repo: https:\/\/github.com\/apollographql\/skills\n    github-tree-sha: b641cd05036ae1833560f3ea3072028e8efa7cae\n    version: 1.1.2\nname: rust-best-practices\n---\n# Rust Best Practices\n\nApply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's [Rust Best Practices Handbook](https:\/\/github.com\/apollographql\/rust-best-practices).\n\n## Best Practices Reference\n\nBefore 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:\n\n- [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)\n- [Chapter 2 - Clippy and Linting](references\/chapter_02.md): Clippy configuration, important lints, workspace lint setup\n- [Chapter 3 - Performance Mindset](references\/chapter_03.md): Profiling, avoiding redundant clones, stack vs heap, zero-cost abstractions\n- [Chapter 4 - Error Handling](references\/chapter_04.md): Result vs panic, thiserror vs anyhow, error hierarchies\n- [Chapter 5 - Automated Testing](references\/chapter_05.md): Test naming, one assertion per test, snapshot testing\n- [Chapter 6 - Generics and Dispatch](references\/chapter_06.md): Static vs dynamic dispatch, trait objects\n- [Chapter 7 - Type State Pattern](references\/chapter_07.md): Compile-time state safety, when to use it\n- [Chapter 8 - Comments vs Documentation](references\/chapter_08.md): When to comment, doc comments, rustdoc\n- [Chapter 9 - Understanding Pointers](references\/chapter_09.md): Thread safety, Send\/Sync, pointer types\n\n## Quick Reference\n\n### Borrowing & Ownership\n- Prefer `&T` over `.clone()` unless ownership transfer is required\n- Use `&str` over `String`, `&[T]` over `Vec<T>` in function parameters\n- Small `Copy` types (\u226424 bytes) can be passed by value\n- Use `Cow<'_, T>` when ownership is ambiguous\n\n### Error Handling\n- Return `Result<T, E>` for fallible operations; avoid `panic!` in production\n- Never use `unwrap()`\/`expect()` outside tests\n- Use `thiserror` for library errors, `anyhow` for binaries only\n- Prefer `?` operator over match chains for error propagation\n\n### Performance\n- Always benchmark with `--release` flag\n- Run `cargo clippy -- -D clippy::perf` for performance hints\n- Avoid cloning in loops; use `.iter()` instead of `.into_iter()` for Copy types\n- Prefer iterators over manual loops; avoid intermediate `.collect()` calls\n\n### Linting\nRun regularly: `cargo clippy --all-targets --all-features --locked -- -D warnings`\n\nKey lints to watch:\n- `redundant_clone` - unnecessary cloning\n- `large_enum_variant` - oversized variants (consider boxing)\n- `needless_collect` - premature collection\n\nUse `#[expect(clippy::lint)]` over `#[allow(...)]` with justification comment.\n\n### Testing\n- Name tests descriptively: `process_should_return_error_when_input_empty()`\n- One assertion per test when possible\n- Use doc tests (`\/\/\/`) for public API examples\n- Consider `cargo insta` for snapshot testing generated output\n\n### Generics & Dispatch\n- Prefer generics (static dispatch) for performance-critical code\n- Use `dyn Trait` only when heterogeneous collections are needed\n- Box at API boundaries, not internally\n\n### Type State Pattern\nEncode valid states in the type system to catch invalid operations at compile time:\n```rust\nstruct Connection<State> { \/* ... *\/ _state: PhantomData<State> }\nstruct Disconnected;\nstruct Connected;\n\nimpl Connection<Connected> {\n    fn send(&self, data: &[u8]) { \/* only connected can send *\/ }\n}\n```\n\n### Documentation\n- `\/\/` comments explain *why* (safety, workarounds, design rationale)\n- `\/\/\/` doc comments explain *what* and *how* for public APIs\n- Every `TODO` needs a linked issue: `\/\/ TODO(#42): ...`\n- Enable `#![deny(missing_docs)]` for libraries\n","use_cases":["Writing new Rust code or functions","Reviewing or refactoring existing Rust code","Deciding between borrowing vs cloning or ownership patterns","Implementing error handling with Result types","Optimizing Rust code for performance","Writing tests or documentation for Rust projects"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/sajaddp\/sonjoo-seo-audit\/blob\/b53a2eae486acf635a0b1dbd76be236343deb4b2\/.agents\/skills\/rust-best-practices\/SKILL.md","source_commit":"b53a2eae486acf635a0b1dbd76be236343deb4b2","license":null,"version":null,"listing_approved_at":"2026-11-30","created_at":"2026-11-29T11:05:03.000000Z","updated_at":"2026-11-30T06:51:59.000000Z","seo_title":"Rust Best Practices Guide","seo_description":"Improve your Rust code quality with Apollo's best practices for borrowing, error handling, performance, and linting using Clippy.","seo_keywords":["Rust best practices","Rust coding style","Rust error handling","Rust performance optimization","Clippy linting","Rust ownership patterns","Rust iterator usage","Rust test writing"]}}