coding-guidelines
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseRust Coding Guidelines (50 Core Rules)
Rust编码指南(50条核心规则)
Naming (Rust-Specific)
命名(Rust专属)
| Rule | Guideline |
|---|---|
No | |
| Iterator convention | |
| Conversion naming | |
| Static var prefix | |
| 规则 | 指南 |
|---|---|
不要使用 | 使用 |
| 迭代器约定 | |
| 转换命名 | |
| 静态变量前缀 | |
Data Types
数据类型
| Rule | Guideline |
|---|---|
| Use newtypes | |
| Prefer slice patterns | |
| Pre-allocate | |
| Avoid Vec abuse | Use arrays for fixed sizes |
| 规则 | 指南 |
|---|---|
| 使用新类型 | 用 |
| 优先使用切片模式 | |
| 预分配内存 | |
| 避免滥用Vec | 固定大小场景使用数组 |
Strings
字符串
| Rule | Guideline |
|---|---|
| Prefer bytes | |
Use | When might modify borrowed data |
Use | Over string concatenation with |
| Avoid nested iteration | |
| 规则 | 指南 |
|---|---|
| 优先使用字节 | 处理ASCII时用 |
使用 | 可能修改借用数据时使用 |
使用 | 替代 |
| 避免嵌套迭代 | 字符串的 |
Error Handling
错误处理
| Rule | Guideline |
|---|---|
Use | Not |
| When value guaranteed |
| Assertions for invariants | |
| 规则 | 指南 |
|---|---|
使用 | 而非 |
优先使用 | 当值有保证时使用 |
| 用断言维护不变量 | 在函数入口处使用 |
Memory
内存
| Rule | Guideline |
|---|---|
| Meaningful lifetimes | |
| Avoid panic |
| Shadowing for transformation | |
| 规则 | 指南 |
|---|---|
| 有意义的生命周期 | 使用 |
对RefCell使用 | 避免恐慌 |
| 用变量遮蔽实现转换 | |
Concurrency
并发
| Rule | Guideline |
|---|---|
| Identify lock ordering | Prevent deadlocks |
| Atomics for primitives | Not Mutex for bool/usize |
| Choose memory order carefully | Relaxed/Acquire/Release/SeqCst |
| 规则 | 指南 |
|---|---|
| 明确锁的顺序 | 防止死锁 |
| 原类型使用原子操作 | 布尔/无符号整数类型不要用Mutex |
| 谨慎选择内存顺序 | Relaxed/Acquire/Release/SeqCst |
Async
异步
| Rule | Guideline |
|---|---|
| Sync for CPU-bound | Async is for I/O |
| Don't hold locks across await | Use scoped guards |
| 规则 | 指南 |
|---|---|
| CPU密集型任务用同步 | 异步适用于I/O密集型任务 |
| 不要在await期间持有锁 | 使用作用域守卫 |
Macros
宏
| Rule | Guideline |
|---|---|
| Avoid unless necessary | Prefer functions/generics |
| Follow Rust syntax | Macro input should look like Rust |
| 规则 | 指南 |
|---|---|
| 非必要不使用 | 优先选择函数/泛型 |
| 遵循Rust语法 | 宏输入应符合Rust语法风格 |
Deprecated → Better
已废弃 → 更优方案
| Deprecated | Better | Since |
|---|---|---|
| | 1.70 |
| | 1.80 |
| | - |
| | - |
| | - |
| | 2018 |
| 已废弃 | 更优方案 | 起始版本 |
|---|---|---|
| | 1.70 |
| | 1.80 |
| | - |
| | - |
| | - |
| | 2018 |
Quick Reference
快速参考
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.
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专属规则。