Skip to content
cccskills
FreePublished on GitHub

rust-dev

Rust 开发规范(聚焦 Axum + Tokio 后端栈),包含错误处理、异步陷阱、模块组织、测试规范等。当操作 .rs、Cargo.toml、Cargo.lock 文件时自动激活。

The English summary is being prepared. Showing the original description.

See how to install

Files included (1)

  • SKILL.md3.2 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

<instructions>

Rust 开发规范

参考来源: 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_caseuser_service.rs
函数/变量snake_casefetch_user, db_pool
类型/Trait/EnumUpperCamelCaseUserRepo, AppError
常量/静态SCREAMING_SNAKE_CASEMAX_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::Mutex
  • Rc/RefCell 不能跨 .await:违反 Send 边界,改 Arc / 缩小作用域
  • tokio::select! 分支必须取消安全:不确定时把状态保存到 select! 外部
// ✅ 阻塞调用包到 spawn_blocking
let data = tokio::task::spawn_blocking(|| std::fs::read("big.bin")).await??;

Axum 实战要点

  • State 一律 Arc 包装,不要克隆裸结构体
  • Handler 错误类型实现 IntoResponse,统一对外 status + json
  • 路由按模块拆 fn routes() -> Router<AppState> + Router::nest
  • 超时/限流/日志/CORS 走 tower-http 中间件

性能优化建议

场景解决方案
循环内字符串拼接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 红线

业务代码原则禁用 unsafe。必须用时(FFI / 极端性能):

  • 块前必须有 // SAFETY: ... 注释,说明保持的不变量
  • 缩小 unsafe 块到最小范围,封装成 safe API 对外暴露

📋 本回复遵循:rust-dev - [章节]

</instructions>

Reviews

No reviews yet. Tell others what you thought.

Comments

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.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

当设计或修改 REST API 响应结构、处理 API 返回值时触发。防止 API 设计缺陷导致的字段错位、类型歧义等问题。

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

当设计或修改 REST API 响应结构、处理 API 返回值,或生成 Excel/CSV/PDF/对账文件等下游产物时触发。防止 API 设计缺陷导致的字段错位、类型歧义,以及生成产物时关键字段缺失但静默成功的问题。

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

网关/代理/WAF/CDN 中间件的安全关键词匹配实现规范,防止纯子串匹配误判正常响应内容中的技术术语(如 Cloudflare、502、error)

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

当 API/任务可能执行超过 10 秒(批量数据处理、远程 API 批量调用、全表扫描、跨租户聚合)时触发。防止同步接口被网关 30s 超时切断、用户重复点击触发并发、状态缓存内存泄漏等问题。提供异步任务状态机标准模板。

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

当用户操作 .sh、Dockerfile、Makefile、.yml、.yaml 文件,或在 Markdown 中编写 bash 代码块时触发。提供 Bash 编写规范。

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

Bash 脚本与系统命令规范。禁止行尾注释,强制使用 tee/heredoc 写入,提升命令执行的可维护性。

The English summary is being prepared. Showing the original description.

doccker/cc-use-exp1.1KUpdated Aug 26, 2026

See all skills by doccker

Report a problem with this skill