跳过正文

1. 标识符和注释:identify/comment

·
Rust
rust-lang - 这篇文章属于一个选集。
§ 1: 本文

标识符不能以数字开头,不能使用关键字和保留字。

raw indentifyr# 开头,后面可以使用关键字作为标识符。

举例:

  • foo
  • _identifier
  • r#true
  • Москва
  • 東京

注释有两种类型:

  1. 常规注释: 不显示在 cargo doc 中:

    1. // :单行注释;
    2. /* */ : 块注释;
  2. cargo doc 文档注释:

    1. INNER LINE DOC: //!
    2. INNER BLOCK DOC: /*! */
    3. OUTER LINE DOC: ///
    4. OUTER BLOCK 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(){}
}
rust-lang - 这篇文章属于一个选集。
§ 1: 本文

相关文章

10. 泛型和特性:generic/trait
·
Rust
Rust 泛型和特性
11. 类型协变:type coercion
·
Rust
Rust 高级话题:子类型和类型协变
12. 迭代器:iterator
·
Rust
Rust 迭代器
13. 包和模块:package/crate/module
·
Rust
Rust 项目的包和模块组织结构