标识符 #
不能以数字开头,不能使用关键字和保留字。
- 以
r#
开头的raw indentify
,可以使用关键字作为标识符。
举例:
- foo
- _identifier
- r#true
- Москва
- 東京
标识符命名风格 #
- struct/enum/union 是 Rust 三种自定义类型,它们的类型命名惯例是 CamelCase,否则编译时警告。
- struct 成员的命名惯例是 snake_case;
- trait 类型使用 CameCase 风格;
- enum 的类型名和 variant 成员名都是 CameCase 风格。
- 变量、函数名和函数参数的命名风格是 snake_case 风格。
注释 #
两种类型,均支持嵌套:
-
常规注释: 不在
cargo doc
中显示://
:单行注释;/* */
: 块注释;
-
文档注释:在
cargo doc
中显示:- INNER LINE DOC:
//!
- INNER BLOCK DOC:
/*! */
- OUTER LINE DOC:
///
- OUTER BLOCK DOC:
/** */
- INNER LINE DOC:
INNER
是 module/crate
级别的注释,等效于 #![doc="comment"]
。
OUTER
是紧接着的 item 的注释,等效于 #[doc="comment"]
。
示例:
// https://doc.rust-lang.org/reference/comments.html
//! A doc comment that applies to the implicit anonymous module of this crate
pub mod outer_module {
//! - Inner line doc
//!! - Still an inner line doc (but with a bang at the beginning)
/*! - Inner block doc */
/*!! - Still an inner block doc (but with a bang at the beginning) */
// - Only a comment
/// - Outer line doc (exactly 3 slashes)
//// - Only a comment
/* - Only a comment */
/** - Outer block doc (exactly) 2 asterisks */
/*** - Only a comment */
pub mod inner_module {}
pub mod nested_comments {
/* In Rust /* we can /* nest comments */ */ */
// All three types of block comments can contain or be nested inside any other type:
/* /* */ /** */ /*! */ */
/*! /* */ /** */ /*! */ */
/** /* */ /** */ /*! */ */
pub mod dummy_item {}
}
pub mod degenerate_cases {
// empty inner line doc
//!
// empty inner block doc
/*!*/
// empty line comment
//
// empty outer line doc
///
// empty block comment
/**/
pub mod dummy_item {}
// empty 2-asterisk block isn't a doc block, it is a block comment
/***/
}
/* The next one isn't allowed because outer doc comments
require an item that will receive the doc */
/// Where is my item?
#[warn(dead_code)]
pub fn test(){}
}