]> git.proxmox.com Git - rustc.git/blame - src/doc/book/src/ch03-04-comments.md
New upstream version 1.37.0+dfsg1
[rustc.git] / src / doc / book / src / ch03-04-comments.md
CommitLineData
13cf67c4
XL
1## Comments
2
3All programmers strive to make their code easy to understand, but sometimes
4extra explanation is warranted. In these cases, programmers leave notes, or
5*comments*, in their source code that the compiler will ignore but people
6reading the source code may find useful.
7
8Here’s a simple comment:
9
10```rust
69743fb6 11// hello, world
13cf67c4
XL
12```
13
14In Rust, comments must start with two slashes and continue until the end of the
15line. For comments that extend beyond a single line, you’ll need to include
16`//` on each line, like this:
17
18```rust
19// So we’re doing something complicated here, long enough that we need
20// multiple lines of comments to do it! Whew! Hopefully, this comment will
21// explain what’s going on.
22```
23
24Comments can also be placed at the end of lines containing code:
25
26<span class="filename">Filename: src/main.rs</span>
27
28```rust
29fn main() {
69743fb6 30 let lucky_number = 7; // I’m feeling lucky today
13cf67c4
XL
31}
32```
33
34But you’ll more often see them used in this format, with the comment on a
35separate line above the code it’s annotating:
36
37<span class="filename">Filename: src/main.rs</span>
38
39```rust
40fn main() {
69743fb6 41 // I’m feeling lucky today
13cf67c4
XL
42 let lucky_number = 7;
43}
44```
45
46Rust also has another kind of comment, documentation comments, which we’ll
9fa01778 47discuss in the “Publishing a Crate to Crates.io” section of Chapter 14.