命名规范
概览
| Item | Convention |
|---|---|
| 包/板条箱 Crates | snake_case (but prefer single word) |
| 模块 Modules | snake_case |
| 类型 Types (枚举和结构体也是一种类型) | UpperCamelCase |
| 特型 Traits | UpperCamelCase |
| 函数 Functions | snake_case |
| 方法 Methods | snake_case |
| 通用构造器 General constructors | new or with_more_details |
| 转换构造器 Conversion constructors | from_some_other_type |
| 局部变量 Local variables | snake_case |
| 静态变量 Static variables | SCREAMING_SNAKE_CASE |
| 常量变量 Constant variables | SCREAMING_SNAKE_CASE |
| 类型参数 Type parameters | concise UpperCamelCase, usually single uppercase letter: T |
| 生命周期 Lifetimes | short, lowercase: 'a |
蛇形命名法(snake_case)
全小写,单词间用下划线分隔。
rust
let user_name = "Alice";
fn calculate_total() {}
mod network_utils;
// 文件名: data_processor.rs大驼峰命名法(PascalCase / UpperCamelCase)
单词首字母大写,无分隔符。
rust
struct UserProfile; // 结构体
enum HttpStatus { OK } // 枚举
trait DataSerializer; // 特质
type Result<T>; // 类型别名
fn handle<RequestType>() // 泛型参数需注意,组合单词和缩略词也遵从这种写法,比如Stdin而不是StdIn,Uuid而不是UUID
常量命名法(SCREAMING_SNAKE_CASE)
用于常量(const)和静态变量(static)。
全大写,单词间用下划线分隔。
rust
const MAX_CONNECTIONS: u32 = 100;
static DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);