当设计或修改 REST API 响应结构、处理 API 返回值时触发。防止 API 设计缺陷导致的字段错位、类型歧义等问题。
The English summary is being prepared. Showing the original description.
Rust 开发规范(聚焦 Axum + Tokio 后端栈),包含错误处理、异步陷阱、模块组织、测试规范等。当操作 .rs、Cargo.toml、Cargo.lock 文件时自动激活。
The English summary is being prepared. Showing the original description.
See how to installReview the instructions the agent will receive before you install.
参考来源: Rust API Guidelines、Rust Async Book、tokio.rs、Axum 官方示例
适用范围:后端 Web 服务(Axum + Tokio 路线)。CLI/wasm/嵌入式不在覆盖范围内。
# 格式化
cargo fmt --all
# 静态检查,warning 当 error
cargo clippy --all-targets -- -D warnings
# 测试
cargo test --all-features
版本基线(以 2026-05 为准,半年后重新校准):edition 2024、Tokio 1.x、Axum 0.8、tower-http 0.6、sqlx 0.8。
| 类型 | 规则 | 示例 |
|---|---|---|
| 模块/文件 | snake_case | user_service.rs |
| 函数/变量 | snake_case | fetch_user, db_pool |
| 类型/Trait/Enum | UpperCamelCase | UserRepo, AppError |
| 常量/静态 | SCREAMING_SNAKE_CASE | MAX_RETRY |
禁止: common、util、helper、base 等无意义模块名。
分场景选边:库用 thiserror,二进制应用用 anyhow,跨边界(handler)用自定义 AppError + IntoResponse。
#[derive(thiserror::Error, Debug)]
pub enum AppError {
#[error("db error: {0}")]
Db(#[from] sqlx::Error),
#[error("not found")]
NotFound,
#[error("bad request: {0}")]
BadRequest(String),
}
禁止生产代码:.unwrap() / .expect() / panic!()(仅测试可用)。clippy 配置:
[lints.clippy]
unwrap_used = "deny"
expect_used = "deny"
panic = "deny"
spawn_blocking:CPU > 10ms、std::fs、同步 DB driver 都不能直接进 async fn.await 持有 std::sync::Mutex:用 tokio::sync::MutexRc/RefCell 不能跨 .await:违反 Send 边界,改 Arc / 缩小作用域tokio::select! 分支必须取消安全:不确定时把状态保存到 select! 外部// ✅ 阻塞调用包到 spawn_blocking
let data = tokio::task::spawn_blocking(|| std::fs::read("big.bin")).await??;
Arc 包装,不要克隆裸结构体IntoResponse,统一对外 status + jsonfn routes() -> Router<AppState> + Router::nest| 场景 | 解决方案 |
|---|---|
| 循环内字符串拼接 | String::with_capacity + push_str |
| Vec 频繁扩容 | Vec::with_capacity(n) |
循环内 .clone() 大对象 | 借用或 Arc<T> |
Box<dyn Trait> 滥用 | 优先泛型 <T: Trait> 静态分发 |
| 阻塞 IO 在 async 里 | tokio::task::spawn_blocking |
业务代码原则禁用 unsafe。必须用时(FFI / 极端性能):
// SAFETY: ... 注释,说明保持的不变量unsafe 块到最小范围,封装成 safe API 对外暴露</instructions>📋 本回复遵循:
rust-dev- [章节]
No reviews yet. Tell others what you thought.
No login needed. Share how it worked for you, ask a question, or note where it did not run.
No comments yet. Be the first to write one.
当设计或修改 REST API 响应结构、处理 API 返回值时触发。防止 API 设计缺陷导致的字段错位、类型歧义等问题。
The English summary is being prepared. Showing the original description.
当设计或修改 REST API 响应结构、处理 API 返回值,或生成 Excel/CSV/PDF/对账文件等下游产物时触发。防止 API 设计缺陷导致的字段错位、类型歧义,以及生成产物时关键字段缺失但静默成功的问题。
The English summary is being prepared. Showing the original description.
网关/代理/WAF/CDN 中间件的安全关键词匹配实现规范,防止纯子串匹配误判正常响应内容中的技术术语(如 Cloudflare、502、error)
The English summary is being prepared. Showing the original description.
当 API/任务可能执行超过 10 秒(批量数据处理、远程 API 批量调用、全表扫描、跨租户聚合)时触发。防止同步接口被网关 30s 超时切断、用户重复点击触发并发、状态缓存内存泄漏等问题。提供异步任务状态机标准模板。
The English summary is being prepared. Showing the original description.
当用户操作 .sh、Dockerfile、Makefile、.yml、.yaml 文件,或在 Markdown 中编写 bash 代码块时触发。提供 Bash 编写规范。
The English summary is being prepared. Showing the original description.
Bash 脚本与系统命令规范。禁止行尾注释,强制使用 tee/heredoc 写入,提升命令执行的可维护性。
The English summary is being prepared. Showing the original description.