rust-best-practices by apollographql/skills
npx skills add https://github.com/apollographql/skills --skill rust-best-practices在编写或审查 Rust 代码时应用这些准则。基于 Apollo GraphQL 的 Rust 最佳实践手册。
在审查之前,请熟悉 Apollo 的 Rust 最佳实践。请在同一轮次中并行阅读所有相关章节。在提供反馈时参考这些文件:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
&T 而非 .clone()&str 而非 String,优先使用 &[T] 而非 Vec<T>Copy 类型(≤24 字节)可以按值传递Cow<'_, T>Result<T, E>;在生产环境中避免使用 panic!unwrap()/expect()thiserror,仅二进制程序使用 anyhow? 操作符而非 match 链--release 标志进行基准测试cargo clippy -- -D clippy::perf 以获取性能提示.iter() 而非 .into_iter().collect() 调用定期运行:cargo clippy --all-targets --all-features --locked -- -D warnings
需要关注的关键检查项:
redundant_clone - 不必要的克隆large_enum_variant - 过大的变体(考虑使用装箱)needless_collect - 过早的集合操作使用 #[expect(clippy::lint)] 而非 #[allow(...)],并附上理由注释。
process_should_return_error_when_input_empty()///)作为公共 API 的示例cargo insta 对生成的输出进行快照测试dyn Trait在类型系统中编码有效状态,以便在编译时捕获无效操作:
struct Connection<State> { /* ... */ _state: PhantomData<State> }
struct Disconnected;
struct Connected;
impl Connection<Connected> {
fn send(&self, data: &[u8]) { /* 只有已连接状态才能发送 */ }
}
// 注释解释 为什么(安全性、变通方案、设计理由)/// 文档注释解释公共 API 的 是什么 和 怎么做TODO 都需要链接到一个问题:// TODO(#42): ...#![deny(missing_docs)]每周安装量
2.1K
仓库
GitHub 星标数
32
首次出现
2026年1月26日
安全审计
安装于
opencode1.8K
codex1.8K
gemini-cli1.7K
github-copilot1.7K
kimi-cli1.6K
amp1.5K
Apply these guidelines when writing or reviewing Rust code. Based on Apollo GraphQL's Rust Best Practices Handbook.
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:
&T over .clone() unless ownership transfer is required&str over String, &[T] over Vec<T> in function parametersCopy types (≤24 bytes) can be passed by valueCow<'_, T> when ownership is ambiguousResult<T, E> for fallible operations; avoid panic! in productionunwrap()/expect() outside teststhiserror for library errors, anyhow for binaries only? operator over match chains for error propagation--release flagcargo clippy -- -D clippy::perf for performance hints.iter() instead of .into_iter() for Copy types.collect() callsRun regularly: cargo clippy --all-targets --all-features --locked -- -D warnings
Key lints to watch:
redundant_clone - unnecessary cloninglarge_enum_variant - oversized variants (consider boxing)needless_collect - premature collectionUse #[expect(clippy::lint)] over #[allow(...)] with justification comment.
process_should_return_error_when_input_empty()///) for public API examplescargo insta for snapshot testing generated outputdyn Trait only when heterogeneous collections are neededEncode 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 */ }
}
// comments explain why (safety, workarounds, design rationale)/// doc comments explain what and how for public APIsTODO needs a linked issue: // TODO(#42): ...#![deny(missing_docs)] for librariesWeekly Installs
2.1K
Repository
GitHub Stars
32
First Seen
Jan 26, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode1.8K
codex1.8K
gemini-cli1.7K
github-copilot1.7K
kimi-cli1.6K
amp1.5K
97,600 周安装