スキル一覧に戻る
huiali

rust-coding

by huiali

An AI expert capability layer for Rust engineering practices, centered on modular skill orchestration and collaborative execution chains. It turns Rust’s core knowledge structures into callable reasoning and decision units, enabling diagnosis, design, and optimization in complex real-world scenarios.

1🍴 1📅 2026年1月25日
GitHubで見るManusで実行

SKILL.md


name: rust-coding description: "Rust 编码规范专家。处理命名, 格式化, 注释, clippy, rustfmt, lint, 代码风格, 最佳实践, naming convention, 代码审查, P.NAM, G.FMT, 怎么命名, 代码规范" globs: ["**/*.rs"]

Rust 编码规范

核心问题

什么样的 Rust 代码才是"惯用的"?

遵循社区约定,让代码可读、可维护。


命名规范 (Rust 特有)

规则正确错误
方法不用 get_ 前缀fn name(&self)fn get_name(&self)
迭代器方法iter() / iter_mut() / into_iter()get_iter()
转换命名as_ (廉价), to_ (昂贵), into_ (所有权)混用
static 变量大写static CONFIG: Configstatic config: Config
const 变量const BUFFER_SIZE: usize = 1024无限制

通用命名

// 变量和函数:snake_case
let max_connections = 100;
fn process_data() { ... }

// 类型和 trait:CamelCase
struct UserSession;
trait Cacheable {}

// 常量:SCREAMING_SAME_CASE
const MAX_CONNECTIONS: usize = 100;
static CONFIG:once_cell::sync::Lazy<Config> = ...

数据类型规范

规则说明示例
用 newtype领域语义struct Email(String)
用 slice 模式模式匹配if let [first, .., last] = slice
预分配避免重新分配Vec::with_capacity(), String::with_capacity()
避免 Vec 滥用固定大小用数组let arr: [u8; 256]

字符串

规则说明
ASCII 数据用 bytes()s.bytes() 优于 s.chars()
可能修改时用 Cow<str>借用或拥有
format! 拼接优于 + 操作符
避免嵌套 contains()O(n*m) 复杂度

错误处理规范

规则说明
? 传播不用 try!()
expect() 优于 unwrap()值确定时
assert! 检查不变量函数入口处
// ✅ 好的错误处理
fn read_config() -> Result<Config, ConfigError> {
    let content = std::fs::read_to_string("config.toml")
        .map_err(ConfigError::from)?;
    toml::from_str(&content)
        .map_err(ConfigError::parse)
}

// ❌ 避免
fn read_config() -> Config {
    std::fs::read_to_string("config.toml").unwrap()  // panic!
}

内存与生命周期

规则说明
生命周期命名有意义'src, 'ctx 而非 'a
RefCelltry_borrow避免 panic
用 shadowing 转换let x = x.parse()?

并发规范

规则说明
确定锁顺序防止死锁
原子类型用于原语不用 Mutex<bool>
谨慎选择内存序Relaxed/Acquire/Release/SeqCst

Async 规范

规则说明
CPU-bound 用同步Async 适用于 I/O
不要跨 await 持有锁使用 scoped guard

宏规范

规则说明
避免宏(除非必要)优先用函数/泛型
宏输入像 Rust可读性优先

废弃模式 → 推荐

废弃推荐版本
lazy_static!std::sync::OnceLock1.70
once_cell::Lazystd::sync::LazyLock1.80
std::sync::mpsccrossbeam::channel-
std::sync::Mutexparking_lot::Mutex-
failure/error-chainthiserror/anyhow-
try!()? operator2018

Clippy 规范

[package]
edition = "2024"
rust-version = "1.85"

[lints.rust]
unsafe_code = "warn"

[lints.clippy]
all = "warn"
pedantic = "warn"

常用 Clippy 规则

Lint说明
clippy::all启用所有警告
clippy::pedantic更严格的检查
clippy::unwrap_used避免 unwrap
clippy::expect_used优先 expect
clippy::clone_on_ref_ptr避免 clone Arc

格式化 (rustfmt)

# 使用默认配置即可
rustfmt src/lib.rs

# 检查格式
rustfmt --check src/lib.rs

# 配置文件 .rustfmt.toml
max_line_width = 100
tab_spaces = 4
edition = "2024"

文档规范

/// 模块文档
//! 本模块处理用户认证...

/// 结构体文档
/// 
/// # Examples
/// ```
/// let user = User::new("name");
/// ```
pub struct User { ... }

/// 方法文档
/// 
/// # Arguments
/// 
/// * `name` - 用户名
/// 
/// # Returns
/// 
/// 初始化后的用户实例
/// 
/// # Panics
/// 
/// 当用户名为空时 panic
pub fn new(name: &str) -> Self { ... }

快速参考

命名: snake_case (fn/var), CamelCase (type), SCREAMING_CASE (const)
格式: rustfmt (just use it)
文档: /// for public items, //! for module docs
Lint: #![warn(clippy::all)]

代码审查清单

  • 命名符合 Rust 惯例
  • 使用 ? 而非 unwrap()
  • 避免不必要的 clone()
  • unsafe 块有 SAFETY 注释
  • 公共 API 有文档注释
  • 运行 cargo clippy
  • 运行 cargo fmt

スコア

総合スコア

70/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

+10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です