rust-patterns

Solid

地道的Rust模式、所有权、错误处理、特质、并发,以及构建安全、高性能应用程序的最佳实践。

AI & Automation 201,447 stars 30903 forks Updated yesterday MIT

Install

View on GitHub

Quality Score: 95/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Rust 开发模式 构建安全、高性能且可维护应用程序的惯用 Rust 模式和最佳实践。 ## 何时使用 * 编写新的 Rust 代码时 * 评审 Rust 代码时 * 重构现有 Rust 代码时 * 设计 crate 结构和模块布局时 ## 工作原理 此技能在六个关键领域强制执行惯用的 Rust 约定:所有权和借用,用于在编译时防止数据竞争;`Result`/`?` 错误传播,库使用 `thiserror` 而应用程序使用 `anyhow`;枚举和穷尽模式匹配,使非法状态无法表示;用于零成本抽象的 trait 和泛型;通过 `Arc<Mutex<T>>`、通道和 async/await 实现的安全并发;以及按领域组织的最小化 `pub` 接口。 ## 核心原则 ### 1. 所有权和借用 Rust 的所有权系统在编译时防止数据竞争和内存错误。 ```rust // Good: Pass references when you don't need ownership fn process(data: &[u8]) -> usize { data.len() } // Good: Take ownership only when you need to store or consume fn store(data: Vec<u8>) -> Record { Record { payload: data } } // Bad: Cloning unnecessarily to avoid borrow checker fn process_bad(data: &Vec<u8>) -> usize { let cloned = data.clone(); // Wasteful — just borrow cloned.len() } ``` ### 使用 `Cow` 实现灵活的所有权 ```rust use std::borrow::Cow; fn normalize(input: &str) -> Cow<'_, str> { if input.contains(' ') { Cow::Owned(input.replace(' ', "_")) } else { Cow::Borrowed(input) // Zero-cost when no mutation needed } } ``` ## 错误处理 ### 使用 `Result` 和 `?` —— 切勿在生产环境中使用 `unwrap()` ```rust // Good: Propagate errors with context use anyhow::{Context, Result}; fn load_config(path: &str) -> Result<Config> { let content = std::fs::read_to_string(path) .with_context(|| format!("failed to read config from {path}"))?; let config: Config = toml::from_str(&content) .with_context(|| format!("failed to parse config from {path}"))...

Details

Author
affaan-m
Repository
affaan-m/everything-claude-code
Created
4 months ago
Last Updated
yesterday
Language
JavaScript
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

AI & Automation Solid

rust-best-practices

Helps write high-quality, idiomatic Rust code following Microsoft Pragmatic Rust Guidelines. Use when writing, reviewing, optimizing Rust code, or dealing with lifetimes, borrow checker, memory safety, cargo, and async Rust.

145 Updated today
majiayu000
AI & Automation Solid

rust-engineer

Writes, reviews, and debugs idiomatic Rust code with memory safety and zero-cost abstractions. Implements ownership patterns, manages lifetimes, designs trait hierarchies, builds async applications with tokio, and structures error handling with Result/Option. Use when building Rust applications, solving ownership or borrowing issues, designing trait-based APIs, implementing async/await concurrency, creating FFI bindings, or optimizing for performance and memory safety. Invoke for Rust, Cargo, ownership, borrowing, lifetimes, async Rust, tokio, zero-cost abstractions, memory safety, systems programming.

9,537 Updated 1 weeks ago
Jeffallan
AI & Automation Listed

rust-standards

Write idiomatic Rust code. Use when writing Rust code.

2 Updated today
kimgoetzke
Code & Development Listed

neo-rust

Use this skill when writing, refactoring, debugging, or auditing Rust code. Trigger for .rs files, Cargo projects, ownership/borrowing/lifetime issues, Result/Option error handling, unnecessary clone/performance work, unsafe code review, or modern Rust architecture.

5 Updated yesterday
Benknightdark
AI & Automation Listed

rust-development

Rust development best practices for the Guts project - idiomatic code, error handling, async patterns, and commonware integration

335 Updated today
aiskillstore