mcpField
R
rust-development

rust-development

github.com/laurigates开发59104 次安装1 天前更新
代码仓库

关于 rust-development

面向 Rust 项目开发与维护者的参考技能,涵盖惯用写法、所有权与内存安全、异步并发、错误处理、性能和测试。

主要功能

  1. 01所有权与内存安全
  2. 02异步编程与并发
  3. 03错误处理与类型安全
  4. 04性能分析与优化
  5. 05测试与质量实践

适用场景

  1. 01设计 Rust 模块和 trait
  2. 02开发异步网络应用
  3. 03为 Rust 项目选择 crate
  4. 04分析和测试 Rust 代码

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/laurigates/claude-plugins --skill rust-development

安装后在 Claude Code 中输入/rust-development确认已加载。

附带文件REFERENCE.md

详细介绍

Rust 开发

面向现代系统编程的 Rust 专业知识,专注于内存安全、无畏并发和零成本抽象。

适用场景

在以下情况下使用此技能……在以下情况下改用同系列技能……
编写 Rust 代码、处理所有权和生命周期、使用 async/await详细配置 lint 规则——使用 clippy-advanced
从生态系统中选择 crate(Tokio、Serde)检测未使用的依赖项——使用 cargo-machete
设计模块结构或 trait 层级通过并行隔离运行测试——使用 cargo-nextest
学习惯用 Rust 模式和 edition 特性生成覆盖率报告——使用 cargo-llvm-cov

核心专长

现代 Rust 生态系统

  • Cargo:构建系统、包管理器和工作区管理
  • Rustc:编译器优化、目标管理和交叉编译
  • Clippy:用于惯用代码和性能改进的 lint 工具
  • Rustfmt:遵循 Rust 风格指南的统一代码格式化
  • Rust-analyzer:集成 LSP 的高级 IDE 支持

语言特性

  • Rust 2024 edition:RPITIT、trait 中的 async fn、impl Trait 改进
  • Const 泛型和编译时计算
  • 泛型关联类型(GAT)
  • Let-else 模式和 if-let 链

核心能力

所有权与内存安全

  • 使用借用和生命周期实现所有权模式
  • 设计零拷贝抽象和高效内存布局
  • 通过 Drop trait 和智能指针(Box、Rc、Arc)应用 RAII 模式
  • 利用内部可变性模式(Cell、RefCell、Mutex、RwLock)
  • 使用 Pin/Unpin 处理自引用结构

异步编程与并发

  • Tokio:用于高性能网络应用的异步运行时
  • async-std:采用熟悉 API 设计的替代异步运行时
  • Futures:可组合的异步抽象和流处理
  • Rayon:使用工作窃取线程池实现数据并行
  • 使用原子操作和内存顺序设计无锁数据结构

错误处理与类型安全

  • 使用 thiserror 和 anyhow 设计全面的错误类型
  • 有效实现 Result<T, E> 和 Option 模式
  • 使用模式匹配进行穷尽式错误处理
  • 应用类型状态模式提供编译时保证

性能优化

  • 使用 cargo-flamegraph、perf 和 criterion 基准测试进行性能分析
  • 使用 SIMD 内建函数和自动向量化进行优化
  • 实现零成本抽象和内联优化
  • 审慎使用 unsafe 代码,并提供恰当的安全性文档

测试与质量保证

  • 单元测试:使用断言的 #[test] 模块
  • 集成测试:使用 tests/ 目录进行端到端验证
  • Criterion:通过统计分析进行微基准测试
  • Miri:检测 unsafe 代码中的未定义行为
  • 模糊测试:使用 cargo-fuzz 进行安全性和健壮性测试

基本命令

# Project setup
cargo new my-project      # Binary crate
cargo new my-lib --lib    # Library crate
cargo init                # Initialize in existing directory
# From a template repo instead of an empty crate — see the cargo-generate skill
cargo generate --git <template-url> --name my-project

# Development workflow
cargo build                      # Debug build
cargo build --release           # Optimized build
cargo run                       # Build and run
cargo run --release             # Run optimized
cargo test                      # Run all tests
cargo test --lib               # Library tests only
cargo bench                     # Run benchmarks

# Code quality
cargo clippy                    # Lint code
cargo clippy -- -W clippy::pedantic  # Stricter lints
cargo fmt                       # Format code
cargo fmt --check              # Check formatting
cargo fix                       # Auto-fix warnings

# Dependencies
cargo add serde --features derive  # Add dependency
cargo update                       # Update deps
cargo audit                        # Security audit
cargo deny check                   # License/advisory check

# Advanced tools
cargo expand                    # Macro expansion
cargo flamegraph               # Profile with flame graph
cargo doc --open               # Generate and open docs
cargo miri test                # Check for UB

# Cross-compilation
rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

最佳实践

惯用 Rust 模式

// Use iterators over manual loops
let sum: i32 = numbers.iter().filter(|x| **x > 0).sum();

// Prefer combinators for Option/Result
let value = config.get("key")
    .and_then(|v| v.parse().ok())
    .unwrap_or_default();

// Use pattern matching effectively
match result {
    Ok(value) if value > 0 => process(value),
    Ok(_) => handle_zero(),
    Err(e) => return Err(e.into()),
}

// Let-else for early returns
let Some(config) = load_config() else {
    return Err(ConfigError::NotFound);
};

项目结构

my-project/
├── Cargo.toml
├── src/
│   ├── lib.rs        # Library root
│   ├── main.rs       # Binary entry point
│   ├── error.rs      # Error types
│   └── modules/
│       └── mod.rs
├── tests/            # Integration tests
├── benches/          # Benchmarks
└── examples/         # Example programs

错误处理

use thiserror::Error;

#[derive(Error, Debug)]
pub enum AppError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("parse error: {message}")]
    Parse { message: String },

    #[error("not found: {0}")]
    NotFound(String),
}

pub type Result<T> = std::result::Result<T, AppError>;

常用 Crate

Crate用途
serde序列化/反序列化
tokio异步运行时
reqwestHTTP 客户端
sqlx异步 SQL
clapCLI 参数解析
tracing日志/诊断
anyhow应用程序错误
thiserror库错误

有关详细的异步模式、unsafe 代码指南、WebAssembly 编译、嵌入式开发和高级调试,请参阅 REFERENCE.md。

评论

支持 Markdown