]>
Commit | Line | Data |
---|---|---|
13cf67c4 XL |
1 | ## Comments |
2 | ||
3 | All programmers strive to make their code easy to understand, but sometimes | |
4 | extra explanation is warranted. In these cases, programmers leave notes, or | |
5 | *comments*, in their source code that the compiler will ignore but people | |
6 | reading the source code may find useful. | |
7 | ||
8 | Here’s a simple comment: | |
9 | ||
10 | ```rust | |
11 | // Hello, world. | |
12 | ``` | |
13 | ||
14 | In Rust, comments must start with two slashes and continue until the end of the | |
15 | line. 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 | ||
24 | Comments can also be placed at the end of lines containing code: | |
25 | ||
26 | <span class="filename">Filename: src/main.rs</span> | |
27 | ||
28 | ```rust | |
29 | fn main() { | |
30 | let lucky_number = 7; // I’m feeling lucky today. | |
31 | } | |
32 | ``` | |
33 | ||
34 | But you’ll more often see them used in this format, with the comment on a | |
35 | separate line above the code it’s annotating: | |
36 | ||
37 | <span class="filename">Filename: src/main.rs</span> | |
38 | ||
39 | ```rust | |
40 | fn main() { | |
41 | // I’m feeling lucky today. | |
42 | let lucky_number = 7; | |
43 | } | |
44 | ``` | |
45 | ||
46 | Rust also has another kind of comment, documentation comments, which we’ll | |
47 | discuss in Chapter 14. |