]> git.proxmox.com Git - rustc.git/blame - src/doc/rust-by-example/src/hello/comment.md
New upstream version 1.68.2+dfsg1
[rustc.git] / src / doc / rust-by-example / src / hello / comment.md
CommitLineData
2c00a5a8
XL
1# Comments
2
dc9dc135 3Any program requires comments, and Rust supports
2c00a5a8
XL
4a few different varieties:
5
6* *Regular comments* which are ignored by the compiler:
f25598a0
FG
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.`
2c00a5a8
XL
12
13```rust,editable
14fn main() {
f25598a0
FG
15 // This is an example of a line comment.
16 // There are two slashes at the beginning of the line.
17 // And nothing written inside these will be read by the compiler.
2c00a5a8
XL
18
19 // println!("Hello, world!");
20
21 // Run it. See? Now try deleting the two slashes, and run it again.
22
f25598a0 23 /*
dc9dc135 24 * This is another type of comment, a block comment. In general,
f25598a0
FG
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! */*/*/
2c00a5a8
XL
30 */
31
32 /*
dc9dc135 33 Note: The previous column of `*` was entirely for style. There's
2c00a5a8
XL
34 no actual need for it.
35 */
36
dc9dc135
XL
37 // You can manipulate expressions more easily with block comments
38 // than with line comments. Try deleting the comment delimiters
39 // to change the result:
2c00a5a8
XL
40 let x = 5 + /* 90 + */ 5;
41 println!("Is `x` 10 or 100? x = {}", x);
42}
2c00a5a8
XL
43```
44
45### See also:
46
47[Library documentation][docs]
48
dc9dc135 49[docs]: ../meta/doc.md