命名
結構
// 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
)