1
1
## Comments
2
2
3
3
All programmers strive to make their code easy to understand, but sometimes
4
- some extra explanation is warranted. In these cases, we leave notes in our
5
- source code that the compiler will ignore. These notes are called * comments* .
4
+ extra explanation is warranted. In these cases, we leave notes in our source
5
+ code that the compiler will ignore but people reading the source code may find
6
+ useful. These notes are called * comments* .
6
7
7
8
Here’s a simple comment:
8
9
9
10
``` rust
10
11
// Hello, world.
11
12
```
12
13
13
- In Rust, comments must start with two slashes, and will last until the end of
14
- the line. For comments that extend beyond a single line, you'll need to include
15
- ` // ` on each line, like this:
14
+ In Rust, comments must start with two slashes and will last until the end of
15
+ the line. For comments that extend beyond a single line, you'll either need to
16
+ include ` // ` on each line, like this:
16
17
17
18
``` rust
18
19
// So we’re doing something complicated here, long enough that we need
19
20
// multiple lines of comments to do it! Whew! Hopefully, this comment will
20
21
// explain what’s going on.
21
22
```
22
23
24
+ Or use ` /* ` at the beginning of your comment and ` */ ` at the end:
25
+
26
+ ``` rust
27
+ /*
28
+ Another style of multiline comment! Most Rust code uses the previous
29
+ style, but you can use this if you prefer.
30
+ */
31
+ ```
23
32
Comments can also be placed at the end of lines of code:
24
33
25
34
``` rust
@@ -54,7 +63,7 @@ usually people who are using your code, so that they know how to interact with
54
63
it. Regular comments won't be shown in ` rustdoc ` generated HTML, so their
55
64
intended audience is people who are reading and editing your code.
56
65
57
- Documentation comments use an extra slash, like this:
66
+ Documentation comments use an extra slash or an extra star , like this:
58
67
59
68
``` rust
60
69
/// The foo function doesn’t really do much.
@@ -67,12 +76,21 @@ fn foo() {
67
76
/// like we did before.
68
77
fn bar () {
69
78
79
+ }
80
+
81
+ /**
82
+ Multiple line documentation comments
83
+ can also use this style.
84
+ */
85
+ fn baz () {
86
+
70
87
}
71
88
```
72
89
73
- The ` rustdoc ` tool would interpret each comment in this example as documenting
90
+ The ` rustdoc ` tool will interpret each comment in this example as documenting
74
91
the thing that follows it. The first comment would be used to document the
75
- ` foo() ` function, and the second comment would document the ` bar() ` function.
92
+ ` foo() ` function, the second comment would document the ` bar() ` function, and
93
+ so forth.
76
94
77
95
Because documentation comments have semantic meaning to ` rustdoc ` , the compiler
78
96
will pay attention to the placement of your documentation comments. For
@@ -94,8 +112,6 @@ This happens because Rust expects a document comment to be associated with
94
112
whatever code comes directly after it, so it sees that a document comment alone
95
113
must be a mistake.
96
114
97
- <!-- Steve-- since documenting free functions in a binary doesn't generate
98
- useful rustdoc, but that's all that has been covered so far, can this section
99
- end with a sentence that says something like "We'll cover generating
100
- documentation for your libraries in chapter XX" or similar? Wherever you talk
101
- about `cargo doc`? /Carol -->
115
+ Providing documentation for libraries is a best practice that the Rust community
116
+ strives to do to be helpful to each other. We'll cover how you can generate
117
+ great API documentation for your library in more detail in Chapter XX.
0 commit comments