|
1 | 1 | # Comments
|
2 | 2 |
|
| 3 | +> **<sup>Lexer</sup>** |
| 4 | +> LINE_COMMENT : |
| 5 | +> `//` (~[`/` `!`] | `//`) ~`\n`<sup>\*</sup> |
| 6 | +> | `//` |
| 7 | +> |
| 8 | +> BLOCK_COMMENT : |
| 9 | +> `/*` (~[`*` `!`] | `**` | _BlockCommentOrDoc_) |
| 10 | +> (_BlockCommentOrDoc_ | ~`*/`)<sup>\*</sup> `*/` |
| 11 | +> | `/**/` |
| 12 | +> | `/***/` |
| 13 | +> |
| 14 | +> OUTER_LINE_DOC : |
| 15 | +> `//!` ~[`\n` _IsolatedCR_]<sup>\*</sup> |
| 16 | +> |
| 17 | +> OUTER_BLOCK_DOC : |
| 18 | +> `/*!` ( _BlockCommentOrDoc_ | ~[`*/` _IsolatedCR_] )<sup>\*</sup> `*/` |
| 19 | +> |
| 20 | +> INNER_LINE_DOC : |
| 21 | +> `///` (~`/` ~[`\n` _IsolatedCR_]<sup>\*</sup>)<sup>?</sup> |
| 22 | +> |
| 23 | +> INNER_BLOCK_DOC : |
| 24 | +> `/**` (~`*` | _BlockCommentOrDoc_ ) |
| 25 | +> (_BlockCommentOrDoc_ | ~[`*/` _IsolatedCR_])<sup>\*</sup> `*/` |
| 26 | +> |
| 27 | +> _BlockCommentOrDoc_ : |
| 28 | +> BLOCK_COMMENT |
| 29 | +> | OUTER_BLOCK_DOC |
| 30 | +> | INNER_BLOCK_DOC |
| 31 | +> |
| 32 | +> _IsolatedCR_ : |
| 33 | +> _A `\r` not followed by a `\n`_ |
| 34 | +
|
| 35 | +## Non-doc comments |
| 36 | + |
3 | 37 | Comments in Rust code follow the general C++ style of line (`//`) and
|
4 | 38 | block (`/* ... */`) comment forms. Nested block comments are supported.
|
5 | 39 |
|
6 |
| -Line comments beginning with exactly _three_ slashes (`///`), and block |
7 |
| -comments (`/** ... */`), are interpreted as a special syntax for `doc` |
8 |
| -[attributes]. That is, they are equivalent to writing |
| 40 | +Non-doc comments are interpreted as a form of whitespace. |
| 41 | + |
| 42 | +## Doc comments |
| 43 | + |
| 44 | +Line doc comments beginning with exactly _three_ slashes (`///`), and block |
| 45 | +doc comments (`/** ... */`), both inner doc comments, are interpreted as a |
| 46 | +special syntax for `doc` [attributes]. That is, they are equivalent to writing |
9 | 47 | `#[doc="..."]` around the body of the comment, i.e., `/// Foo` turns into
|
10 |
| -`#[doc="Foo"]`. |
| 48 | +`#[doc="Foo"]` and `/** Bar */` turns into `#[doc="Bar"]`. |
11 | 49 |
|
12 | 50 | Line comments beginning with `//!` and block comments `/*! ... */` are
|
13 | 51 | doc comments that apply to the parent of the comment, rather than the item
|
14 | 52 | that follows. That is, they are equivalent to writing `#![doc="..."]` around
|
15 | 53 | the body of the comment. `//!` comments are usually used to document
|
16 | 54 | modules that occupy a source file.
|
17 | 55 |
|
18 |
| -Non-doc comments are interpreted as a form of whitespace. |
| 56 | +Isolated CRs (`\r`), i.e. not followed by LF (`\n`), are not allowed in doc |
| 57 | +comments. |
| 58 | + |
| 59 | +## Examples |
| 60 | + |
| 61 | +```rust |
| 62 | +//! A doc comment that applies to the implicit anonymous module of this crate |
| 63 | + |
| 64 | +pub mod outer_module { |
| 65 | + |
| 66 | + //! - Inner line doc |
| 67 | + //!! - Still an inner line doc (but with a bang at the beginning) |
| 68 | + |
| 69 | + /*! - Inner block doc */ |
| 70 | + /*!! - Still an inner block doc (but with a bang at the beginning) */ |
| 71 | + |
| 72 | + // - Only a comment |
| 73 | + /// - Outer line doc (exactly 3 slashes) |
| 74 | + //// - Only a comment |
| 75 | + |
| 76 | + /* - Only a comment */ |
| 77 | + /** - Outer block doc (exactly) 2 asterisks */ |
| 78 | + /*** - Only a comment */ |
| 79 | + |
| 80 | + pub mod inner_module {} |
| 81 | + |
| 82 | + pub mod nested_comments { |
| 83 | + /* In Rust /* we can /* nest comments */ */ */ |
| 84 | + |
| 85 | + // All three types of block comments can contain or be nested inside |
| 86 | + // any other type: |
| 87 | + |
| 88 | + /* /* */ /** */ /*! */ */ |
| 89 | + /*! /* */ /** */ /*! */ */ |
| 90 | + /** /* */ /** */ /*! */ */ |
| 91 | + pub mod dummy_item {} |
| 92 | + } |
| 93 | + |
| 94 | + pub mod degenerate_cases { |
| 95 | + // empty inner line doc |
| 96 | + //! |
| 97 | + |
| 98 | + // empty inner block doc |
| 99 | + /*!*/ |
| 100 | + |
| 101 | + // empty line comment |
| 102 | + // |
| 103 | + |
| 104 | + // empty outer line doc |
| 105 | + /// |
| 106 | + |
| 107 | + // empty block comment |
| 108 | + /**/ |
| 109 | + |
| 110 | + pub mod dummy_item {} |
| 111 | + |
| 112 | + // empty 2-asterisk block isn't a doc block, it is a block comment |
| 113 | + /***/ |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + /* The next one isn't allowed because outer doc comments |
| 118 | + require an item that will receive the doc */ |
| 119 | + |
| 120 | + /// Where is my item? |
| 121 | +# mod boo {} |
| 122 | +} |
| 123 | +``` |
19 | 124 |
|
20 | 125 | [attributes]: attributes.html
|
0 commit comments