|
3 | 3 | Any program requires comments, and Rust supports |
4 | 4 | a few different varieties: |
5 | 5 |
|
6 | | -* *Regular comments* which are ignored by the compiler: |
7 | | - * `// Line comments which go to the end of the line.` |
8 | | - * `/* Block comments which go to the closing delimiter. */` |
9 | | -* *Doc comments* which are parsed into HTML library [documentation][docs]: |
10 | | - * `/// Generate library docs for the following item.` |
11 | | - * `//! Generate library docs for the enclosing item.` |
| 6 | +## Regular Comments |
12 | 7 |
|
| 8 | +These are ignored by the compiler: |
| 9 | + |
| 10 | +* **Line comments**: Start with `//` and continue to the end of the line |
| 11 | +* **Block comments**: Enclosed in `/* ... */` and can span multiple lines |
| 12 | + |
| 13 | +## Documentation Comments (Doc Comments) which are parsed into HTML library [documentation][docs]: |
| 14 | + |
| 15 | + - `///` - Generates docs for the item that follows it |
| 16 | +- `//!` - Generates docs for the enclosing item (typically used at the top of a file or module) |
13 | 17 | ```rust,editable |
| 18 | +
|
14 | 19 | fn main() { |
15 | | - // This is an example of a line comment. |
16 | | - // There are two slashes at the beginning of the line. |
17 | | - // And nothing written after these will be read by the compiler. |
| 20 | + // Line comments start with two slashes. |
| 21 | + // Everything after the slashes is ignored by the compiler. |
18 | 22 |
|
| 23 | + // Example: This line won't execute |
19 | 24 | // println!("Hello, world!"); |
20 | 25 |
|
21 | | - // Run it. See? Now try deleting the two slashes, and run it again. |
| 26 | + // Try removing the slashes above and running the code again. |
22 | 27 |
|
23 | 28 | /* |
24 | | - * This is another type of comment, a block comment. In general, |
25 | | - * line comments are the recommended comment style. But block comments |
26 | | - * are extremely useful for temporarily disabling chunks of code. |
27 | | - * /* Block comments can be /* nested, */ */ so it takes only a few |
28 | | - * keystrokes to comment out everything in this main() function. |
29 | | - * /*/*/* Try it yourself! */*/*/ |
| 29 | + * Block comments are useful for temporarily disabling code. |
| 30 | + * They can also be nested: /* like this */ which makes it easy |
| 31 | + * to comment out large sections quickly. |
30 | 32 | */ |
31 | 33 |
|
32 | 34 | /* |
33 | | - Note: The previous column of `*` was entirely for style. There's |
34 | | - no actual need for it. |
| 35 | + Note: The asterisk column on the left is just for style - |
| 36 | + it's not required by the language. |
35 | 37 | */ |
36 | 38 |
|
37 | | - // Here's another powerful use of block comments: you can uncomment |
38 | | - // and comment a whole block by simply adding or removing a single |
39 | | - // '/' character: |
| 39 | + // Block comments make it easy to toggle code on/off by adding |
| 40 | + // or removing just one slash: |
40 | 41 |
|
41 | | - /* <- add another '/' before the 1st one to uncomment the whole block |
| 42 | + /* <- Add a '/' here to uncomment the entire block below |
42 | 43 |
|
43 | 44 | println!("Now"); |
44 | 45 | println!("everything"); |
45 | 46 | println!("executes!"); |
46 | | - // line comments inside are not affected by either state |
| 47 | + // Line comments inside remain unaffected |
47 | 48 |
|
48 | 49 | // */ |
49 | 50 |
|
50 | | - // You can manipulate expressions more easily with block comments |
51 | | - // than with line comments. Try deleting the comment delimiters |
52 | | - // to change the result: |
| 51 | + // Block comments can also be used within expressions: |
53 | 52 | let x = 5 + /* 90 + */ 5; |
54 | 53 | println!("Is `x` 10 or 100? x = {}", x); |
55 | 54 | } |
|
0 commit comments