{"id":"f40952af-7739-4c87-8450-570ec2573b8a","shortId":"XnKU5Y","kind":"skill","title":"Rust Best Practices","tagline":"Skills skill by Apollographql","description":"# 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\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 (≤24 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","tags":["rust","best","practices","skills","apollographql"],"capabilities":["skill","source-apollographql","category-skills"],"categories":["skills"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/apollographql/skills/rust-best-practices","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"install_from":"skills.sh"}},"qualityScore":"0.300","qualityRationale":"deterministic score 0.30 from registry signals: · indexed on skills.sh · published under apollographql/skills","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill:v1","enrichmentVersion":1,"enrichedAt":"2026-04-23T22:40:14.519Z","embedding":null,"createdAt":"2026-04-18T20:26:05.453Z","updatedAt":"2026-04-23T22:40:14.519Z","lastSeenAt":"2026-04-23T22:40:14.519Z","tsv":"'/apollographql/rust-best-practices).':31 '1':62 '2':78 '24':207 '3':91 '4':107 '42':455 '5':120 '6':133 '7':145 '8':160 '9':172 'abstract':105 'all-featur':302 'all-target':299 'allow':332 'alway':258 'ambigu':220 'anyhow':116,245 'api':358,390,447 'apollo':22,40 'apollographql':7 'appli':11 'assert':127,348 'autom':121 'avoid':96,230,272,290 'base':20 'benchmark':259 'best':2,9,26,32,43 'binari':247 'borrow':68,183 'boundari':391 'box':322,388 'byte':208 'call':293 'cargo':264,297,361 'catch':405 'category-skills' 'chain':253 'chapter':48,61,77,90,106,119,132,144,159,171 'clippi':79,83,265,267,298,329 'clone':70,98,188,273,313,315 'code':19,63,378 'collect':292,324,326,385 'comment':76,161,167,169,335,432,440 'compil':151,409 'compile-tim':150 'configur':84 'connect':413,419,421,428 'consid':321,360 'copi':71,205,283 'cost':104 'cow':215 'critic':377 'd':266,306 'data':425 'deni':457 'descript':339 'design':437 'disconnect':417 'dispatch':136,141,369,373 'doc':168,354,439,459 'document':163,431 'dyn':380 'dynam':140 'e':226 'empti':346 'enabl':456 'encod':397 'enum':317 'error':108,117,221,244,255,343 'everi':448 'exampl':359 'expect':237,328 'explain':433,441 'fallibl':228 'familiar':37 'featur':304 'feedback':60 'file':57 'flag':262 'fn':422 'function':202 'generat':366 'generic':134,368,371 'github.com':30 'github.com/apollographql/rust-best-practices).':29 'graphql':23 'guidelin':13 'handbook':28 'handl':74,109,222 'heap':101 'heterogen':384 'hierarchi':118 'hint':271 'idiom':66 'impl':420 'import':85 'input':345 'insta':362 'instead':278 'intermedi':291 'intern':393 'invalid':406 'issu':453 'iter':75,277,281,286 'justif':334 'key':308 'larg':316 'librari':243,461 'link':452 'lint':81,86,88,294,309,330 'lock':305 'loop':275,289 'manual':288 'match':252 'mindset':93 'miss':458 'name':125,337 'need':387,450 'needless':323 'never':234 'object':143 'one':126,347 'oper':229,250,407 'option/result':73 'output':367 'outsid':238 'overs':319 'ownership':184,190,218 'panic':113,231 'parallel':54 'paramet':203 'pass':211 'pattern':148,396 'per':128,349 'perf':268 'perform':92,257,270,376 'performance-crit':375 'phantomdata':415 'pointer':174,179 'possibl':352 'practic':3,10,27,33,44 'prefer':185,249,285,370 'prematur':325 'process':340 'product':233 'profil':95 'propag':256 'provid':59 'public':357,446 'quick':181 'rational':438 'read':45 'redund':97,312 'refer':34,55,182 'references/chapter_01.md':67 'references/chapter_02.md':82 'references/chapter_03.md':94 'references/chapter_04.md':110 'references/chapter_05.md':123 'references/chapter_06.md':137 'references/chapter_07.md':149 'references/chapter_08.md':164 'references/chapter_09.md':175 'regular':296 'releas':261 'relev':47 'requir':193 'result':111,224 'return':223,342 'review':17,36 'run':263,295 'rust':1,8,18,25,42,411 'rustdoc':170 'safeti':154,177,435 'self':424 'send':423,430 'send/sync':178 'setup':89 'skill':4,5 'small':204 'snapshot':130,364 'source-apollographql' 'stack':99 'state':147,153,395,399,414 'static':138,372 'str':195 'string':197 'struct':412,416,418 'style':64 'system':403 'target':301 'test':122,124,129,131,239,336,338,350,355,365 'thiserror':114,241 'thread':176 'time':152,410 'todo':449,454 'trait':72,142,381 'transfer':191 'turn':52 'type':146,180,206,284,394,402 'u8':426 'understand':173 'unless':189 'unnecessari':314 'unwrap':236 'use':157,194,214,235,240,276,327,353,379 'valid':398 'valu':213 'variant':318,320 'vec':200 'vs':69,100,112,115,139,162 'warn':307 'watch':311 'workaround':436 'workspac':87 'write':15 'zero':103 'zero-cost':102","prices":[{"id":"8ab2ed3d-31cf-42af-a47c-9ff406a00cb0","listingId":"f40952af-7739-4c87-8450-570ec2573b8a","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"apollographql","category":"skills","install_from":"skills.sh"},"createdAt":"2026-04-18T20:26:05.453Z"}],"sources":[{"listingId":"f40952af-7739-4c87-8450-570ec2573b8a","source":"github","sourceId":"apollographql/skills/rust-best-practices","sourceUrl":"https://github.com/apollographql/skills/tree/main/skills/rust-best-practices","isPrimary":false,"firstSeenAt":"2026-04-18T22:17:28.143Z","lastSeenAt":"2026-04-23T18:57:24.295Z"},{"listingId":"f40952af-7739-4c87-8450-570ec2573b8a","source":"skills_sh","sourceId":"apollographql/skills/rust-best-practices","sourceUrl":"https://skills.sh/apollographql/skills/rust-best-practices","isPrimary":true,"firstSeenAt":"2026-04-18T20:26:05.453Z","lastSeenAt":"2026-04-23T22:40:14.519Z"}],"details":{"listingId":"f40952af-7739-4c87-8450-570ec2573b8a","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"apollographql","slug":"rust-best-practices","source":"skills_sh","category":"skills","skills_sh_url":"https://skills.sh/apollographql/skills/rust-best-practices"},"updatedAt":"2026-04-23T22:40:14.519Z"}}