For now, this reference is a best-effort document. We strive for validity and completeness, but are not yet there. In the future, the docs and lang teams will work together to figure out how best to do this. Until then, this is a best-effort attempt. If you find something wrong or missing, file an issue or send in a pull request.

注释

comments Commit: 993393d362cae51584d580f86c4f38d43ae76efc

Lexer
LINE_COMMENT :
      // (~[/ !] | //) ~\n*
   | //

BLOCK_COMMENT :
      /* (~[* !] | ** | BlockCommentOrDoc) (BlockCommentOrDoc | ~*/)* */
   | /**/
   | /***/

INNER_LINE_DOC :
   //! ~[\n IsolatedCR]*

INNER_BLOCK_DOC :
   /*! ( BlockCommentOrDoc | ~[*/ IsolatedCR] )* */

OUTER_LINE_DOC :
   /// (~/ ~[\n IsolatedCR]*)?

OUTER_BLOCK_DOC :
   /** (~* | BlockCommentOrDoc ) (BlockCommentOrDoc | ~[*/ IsolatedCR])* */

BlockCommentOrDoc :
      BLOCK_COMMENT
   | OUTER_BLOCK_DOC
   | INNER_BLOCK_DOC

IsolatedCR :
   A \r not followed by a \n

非文档评论

Rust代码中的注释遵循C ++的line(//)和block(/ * ... * /)注释表现样式。 支持块嵌套注释。

非文档注释被解释为空格的一种形式。

文档注释

完全以三个斜杠(///)开头的行文档注释和块文档注释(/ ** ... * /)两个内部文档注释都被解释为 doc属性的特殊语法。 也就是说,它们相当于围绕注释的主体编写#[doc =“...”],即/// Foo变成#[doc ="Foo"]和/ ** Bar * /变成#[DOC= "Bar"]。

以//!开头的行注释 和块注释/ *! ... * /都是适用于注释的父级的文档注释,而不是紧接着的项。 也就是说,它们相当于在注释正文周围写#![doc ="..."]。 //! 注释通常用于记录占用源文件的模块。

在文档注释中不允许使用隔离的CR(\ r),即不跟随LF(\ n)。

示例

//! 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? # mod boo {} }