命名
结构
// Structs use UpperCamelCase.
pub struct Snafucator {
}
mod snafucators {
// Try to avoid 'stuttering' by repeating
// the module name in the struct name.
// Bad:
pub struct OrderedSnafucator {
}
// Good:
pub struct Ordered {
}
}
性状
// Traits use the same naming principles as
// structs (UpperCamelCase).
trait Read {
fn read_to_snafucator(&self) -> Result<(), Error>;
}
板条箱和模块
// Modules and crates should both use snake_case.
// Crates should try to use single words if possible.
extern crate foo;
mod bar_baz {
mod quux {
}
}
静态变量和常量
// Statics and constants use SCREAMING_SNAKE_CASE.
const NAME: &'static str = "SCREAMING_SNAKE_CASE";
枚举
// Enum types and their variants **both** use UpperCamelCase.
pub enum Option<T> {
Some(T),
None
}
功能和方法
// Functions and methods use snake_case
fn snake_cased_function() {
}
变量绑定
// Regular variables also use snake_case
let foo_bar = "snafu";
寿命
// Lifetimes should consist of a single lower case letter. By
// convention, you should start at 'a, then 'b, etc.
// Good:
struct Foobar<'a> {
x: &'a str
}
// Bad:
struct Bazquux<'stringlife> {
my_str: &'stringlife str
}
缩略语
包含首字母缩略词的变量名称(例如 TCP
)的样式应如下所示:
- 对于
UpperCamelCase
名称,首字母应大写(例如TcpClient
) - 对于
snake_case
名称,应该没有大写(例如tcp_client
) - 对于
SCREAMING_SNAKE_CASE
名称,首字母缩略词应该完全大写(例如TCP_CLIENT
)