coding-guidelines by zhanghandong/rust-skills
npx skills add https://github.com/zhanghandong/rust-skills --skill coding-guidelines| 规则 | 指南 |
|---|---|
不使用 get_ 前缀 | fn name() 而非 fn get_name() |
| 迭代器约定 | iter() / iter_mut() / into_iter() |
| 转换命名 | as_(廉价借用),(昂贵),(获取所有权) |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
to_into_| 静态变量前缀 | static 使用 G_CONFIG,const 无前缀 |
| 规则 | 指南 |
|---|---|
| 使用新类型 | struct Email(String) 用于领域语义 |
| 优先使用切片模式 | if let [first, .., last] = slice |
| 预分配 | Vec::with_capacity(),String::with_capacity() |
| 避免滥用 Vec | 固定大小使用数组 |
| 规则 | 指南 |
|---|---|
| 优先使用字节 | 处理 ASCII 时用 s.bytes() 而非 s.chars() |
使用 Cow<str> | 当可能需要修改借用数据时 |
使用 format! | 而非使用 + 进行字符串拼接 |
| 避免嵌套迭代 | 字符串上的 contains() 是 O(n*m) 复杂度 |
| 规则 | 指南 |
|---|---|
使用 ? 传播 | 而非 try!() 宏 |
expect() 优于 unwrap() | 当值有保证时 |
| 使用断言处理不变量 | 在函数入口使用 assert! |
| 规则 | 指南 |
|---|---|
| 使用有意义的生命周期 | 'src,'ctx 而非仅仅是 'a |
对 RefCell 使用 try_borrow() | 避免恐慌 |
| 使用遮蔽进行转换 | let x = x.parse()? |
| 规则 | 指南 |
|---|---|
| 识别锁顺序 | 防止死锁 |
| 对基本类型使用原子操作 | 对 bool/usize 不使用 Mutex |
| 谨慎选择内存顺序 | Relaxed/Acquire/Release/SeqCst |
| 规则 | 指南 |
|---|---|
| CPU 密集型任务使用同步 | 异步适用于 I/O 操作 |
| 不要在 await 期间持有锁 | 使用作用域守卫 |
| 规则 | 指南 |
|---|---|
| 除非必要,避免使用 | 优先选择函数/泛型 |
| 遵循 Rust 语法 | 宏输入应看起来像 Rust 代码 |
| 已弃用 | 更优方案 | 自版本 |
|---|---|---|
lazy_static! | std::sync::OnceLock | 1.70 |
once_cell::Lazy | std::sync::LazyLock | 1.80 |
std::sync::mpsc | crossbeam::channel | - |
std::sync::Mutex | parking_lot::Mutex | - |
failure/error-chain | thiserror/anyhow | - |
try!() | ? 运算符 | 2018 |
Naming: snake_case (fn/var), CamelCase (type), SCREAMING_CASE (const)
Format: rustfmt (just use it)
Docs: /// for public items, //! for module docs
Lint: #![warn(clippy::all)]
Claude 非常了解 Rust 的约定。这些是非显而易见的 Rust 特有规则。
每周安装量
1.0K
代码仓库
GitHub 星标数
920
首次出现
2026 年 1 月 20 日
安全审计
安装于
opencode894
codex854
gemini-cli834
github-copilot810
amp692
kimi-cli690
| Rule | Guideline |
|---|---|
No get_ prefix | fn name() not fn get_name() |
| Iterator convention | iter() / iter_mut() / into_iter() |
| Conversion naming | as_ (cheap &), to_ (expensive), into_ (ownership) |
| Static var prefix | G_CONFIG for static, no prefix for const |
| Rule | Guideline |
|---|---|
| Use newtypes | struct Email(String) for domain semantics |
| Prefer slice patterns | if let [first, .., last] = slice |
| Pre-allocate | Vec::with_capacity(), String::with_capacity() |
| Avoid Vec abuse | Use arrays for fixed sizes |
| Rule | Guideline |
|---|---|
| Prefer bytes | s.bytes() over s.chars() when ASCII |
Use Cow<str> | When might modify borrowed data |
Use format! | Over string concatenation with + |
| Avoid nested iteration | contains() on string is O(n*m) |
| Rule | Guideline |
|---|---|
Use ? propagation | Not try!() macro |
expect() over unwrap() | When value guaranteed |
| Assertions for invariants | assert! at function entry |
| Rule | Guideline |
|---|---|
| Meaningful lifetimes | 'src, 'ctx not just 'a |
try_borrow() for RefCell | Avoid panic |
| Shadowing for transformation | let x = x.parse()? |
| Rule | Guideline |
|---|---|
| Identify lock ordering | Prevent deadlocks |
| Atomics for primitives | Not Mutex for bool/usize |
| Choose memory order carefully | Relaxed/Acquire/Release/SeqCst |
| Rule | Guideline |
|---|---|
| Sync for CPU-bound | Async is for I/O |
| Don't hold locks across await | Use scoped guards |
| Rule | Guideline |
|---|---|
| Avoid unless necessary | Prefer functions/generics |
| Follow Rust syntax | Macro input should look like Rust |
| Deprecated | Better | Since |
|---|---|---|
lazy_static! | std::sync::OnceLock | 1.70 |
once_cell::Lazy | std::sync::LazyLock | 1.80 |
std::sync::mpsc | crossbeam::channel | - |
std::sync::Mutex |
Naming: snake_case (fn/var), CamelCase (type), SCREAMING_CASE (const)
Format: rustfmt (just use it)
Docs: /// for public items, //! for module docs
Lint: #![warn(clippy::all)]
Claude knows Rust conventions well. These are the non-obvious Rust-specific rules.
Weekly Installs
1.0K
Repository
GitHub Stars
920
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode894
codex854
gemini-cli834
github-copilot810
amp692
kimi-cli690
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
parking_lot::Mutex |
| - |
failure/error-chain | thiserror/anyhow | - |
try!() | ? operator | 2018 |