Agent Skillsplanetaryescape/mxr › rust-best-practices

rust-best-practices

GitHub

基于Apollo GraphQL手册的Rust最佳实践指南。用于编写、审查或重构Rust代码,涵盖所有权借用、错误处理、性能优化、Clippy lint、测试及类型系统等规范,提供具体代码示例和配置建议。

.agents/skills/rust-best-practices/SKILL.md planetaryescape/mxr

触发场景

编写新的Rust代码或函数 审查或重构现有Rust代码 决定使用借用还是克隆 实现Result类型的错误处理 优化Rust代码性能 编写Rust项目测试或文档

安装

npx skills add planetaryescape/mxr --skill rust-best-practices -g -y
更多选项

非标准路径

npx skills add https://github.com/planetaryescape/mxr/tree/main/.agents/skills/rust-best-practices -g -y

不安装直接使用

npx skills use planetaryescape/mxr@rust-best-practices

指定 Agent (Claude Code)

npx skills add planetaryescape/mxr --skill rust-best-practices -a claude-code -g -y

安装 repo 全部 skill

npx skills add planetaryescape/mxr --all -g -y

预览 repo 内 skill

npx skills add planetaryescape/mxr --list

SKILL.md

Frontmatter
{
    "name": "rust-best-practices",
    "license": "MIT",
    "metadata": {
        "author": "apollographql",
        "version": "1.1.0"
    },
    "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.\n",
    "allowed-tools": "Bash(cargo:*) Bash(rustc:*) Bash(rustfmt:*) Bash(clippy:*) Read Write Edit Glob Grep",
    "compatibility": "Rust 1.70+, Cargo"
}

Rust Best Practices

Apply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's Rust Best Practices Handbook.

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:

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:

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

版本历史

  • c4ada04 当前 2026-07-30 20:16

同 Skill 集合

.agents/skills/email-standards/SKILL.md
.agents/skills/mxr-development/SKILL.md
.agents/skills/mxr/SKILL.md
.agents/skills/rust-async-patterns/SKILL.md
.agents/skills/rust-learner/SKILL.md
.agents/skills/rust-engineer/SKILL.md

元信息

文件数
0
版本
c4ada04
Hash
80eeb8ab
收录时间
2026-07-30 20:16

首页 - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-08-01 00:35
浙ICP备14020137号-1 $访客地图$