From rust
Rust核心开发规范 - Rust 2024 edition、所有权系统、错误处理(Result/Option/?操作符)、模式匹配、cargo/clippy/rustfmt工具链。所有Rust编码、调试、测试的基础规范,其他Rust技能的前置依赖。
npx claudepluginhub lazygophers/ccplugin --plugin rustThis skill uses the workspace's default tool permissions.
- **rust:dev** - 开发阶段使用
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Searches prompts.chat for AI prompt templates by keyword or category, retrieves by ID with variable handling, and improves prompts via AI. Use for discovering or enhancing prompts.
Guides MCP server integration in Claude Code plugins via .mcp.json or plugin.json configs for stdio, SSE, HTTP types, enabling external services as tools.
&T 或一个 &mut T// 库代码:thiserror 定义类型化错误
use thiserror::Error;
#[derive(Error, Debug)]
pub enum AppError {
#[error("not found: {0}")]
NotFound(String),
#[error("invalid input: {0}")]
InvalidInput(String),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Database(#[from] sqlx::Error),
}
// 应用代码:anyhow 快速传播
use anyhow::{Context, Result};
fn load_config(path: &str) -> Result<Config> {
let content = std::fs::read_to_string(path)
.context("failed to read config file")?;
let config: Config = toml::from_str(&content)
.context("failed to parse config")?;
Ok(config)
}
// let-else 模式(Rust 2024)
fn parse_id(input: &str) -> Result<u64> {
let Some(id_str) = input.strip_prefix("id:") else {
anyhow::bail!("invalid format: expected 'id:<number>'");
};
Ok(id_str.parse()?)
}
禁止行为:
.unwrap() / .expect() 处理可恢复错误panic!() 处理预期的错误情况Box<dyn Error> 作为库的公共错误类型| 用途 | 推荐库 | 说明 |
|---|---|---|
| 异步运行时 | tokio 1.x | 标准异步运行时 |
| 序列化 | serde + serde_json | 序列化/反序列化 |
| 错误处理 | thiserror 2.x / anyhow 1.x | 库/应用错误 |
| 日志 | tracing | 结构化日志和追踪 |
| HTTP 框架 | axum 0.8+ | 基于 tower 的 Web 框架 |
| HTTP 客户端 | reqwest | 异步 HTTP 客户端 |
| 数据库 | sqlx | 编译时检查的异步 SQL |
| CLI | clap 4.x | 命令行参数解析 |
| 测试 | proptest / criterion | 属性测试 / 基准测试 |
| 并行 | rayon | 数据并行 |
# 格式化
cargo fmt
# Lint(严格模式)
cargo clippy -- -W clippy::all -W clippy::pedantic
# 测试(推荐 nextest)
cargo nextest run
# 安全审计
cargo audit
cargo deny check
# Unsafe 验证
cargo +nightly miri test
// if-let-chains(Rust 2024 stable)
if let Some(user) = get_user(id)
&& user.is_active
&& user.role == Role::Admin
{
grant_access(&user);
}
// let-else
let Ok(value) = input.parse::<i64>() else {
return Err(AppError::InvalidInput("not a number".into()));
};
// async fn in traits(无需 #[async_trait])
trait Database: Send + Sync {
async fn get(&self, id: u64) -> Result<Option<Row>>;
async fn insert(&self, row: &Row) -> Result<()>;
}
| AI 可能的解释 | 实际检查 |
|---|---|
| "unwrap 这里不会失败" | ✅ 是否使用 ? 或有详细 expect 说明? |
| "clone 更简单" | ✅ 是否可以借用或使用 Cow? |
| "String 参数方便" | ✅ 函数参数是否应为 &str? |
| "#[async_trait] 必须用" | ✅ Rust 1.75+ 原生支持 async fn in traits |
| "Box<dyn Error> 通用" | ✅ 库代码是否应使用 thiserror? |
| "cargo test 够用" | ✅ 是否使用 cargo-nextest + proptest? |
my-project/
├── Cargo.toml # edition = "2024"
├── src/
│ ├── lib.rs # 库入口
│ ├── main.rs # 二进制入口(可选)
│ ├── error.rs # 错误定义
│ ├── config.rs # 配置
│ └── models/ # 数据模型
├── tests/ # 集成测试
│ └── integration.rs
├── benches/ # 基准测试
│ └── bench.rs
├── examples/ # 示例
└── .cargo/
└── config.toml # Cargo 配置
[package]
name = "my-project"
version = "0.1.0"
edition = "2024"
rust-version = "1.85"
[dependencies]
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
thiserror = "2"
anyhow = "1"
tracing = "0.1"
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
proptest = "1"
tokio-test = "0.4"
[profile.release]
lto = "fat"
codegen-units = 1
strip = true
[[bench]]
name = "benchmarks"
harness = false
cargo fmt --check 通过cargo clippy -- -W clippy::all -W clippy::pedantic 无警告thiserror 定义错误anyhow + context().unwrap() 处理可恢复错误cargo audit 无漏洞cargo deny check 通过unsafezeroize