Skip to content

Commit 9818cfe

Browse files
committed
Resolve comments in... comments
I added a bit about the other style of comments being possible since it's preferred for accessibility reasons: rust-lang/rfcs#1574 (comment)
1 parent 0a6b7ac commit 9818cfe

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/ch03-06-comments.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
## Comments
22

33
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*.
67

78
Here’s a simple comment:
89

910
```rust
1011
// Hello, world.
1112
```
1213

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:
1617

1718
```rust
1819
// So we’re doing something complicated here, long enough that we need
1920
// multiple lines of comments to do it! Whew! Hopefully, this comment will
2021
// explain what’s going on.
2122
```
2223

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+
```
2332
Comments can also be placed at the end of lines of code:
2433

2534
```rust
@@ -54,7 +63,7 @@ usually people who are using your code, so that they know how to interact with
5463
it. Regular comments won't be shown in `rustdoc` generated HTML, so their
5564
intended audience is people who are reading and editing your code.
5665

57-
Documentation comments use an extra slash, like this:
66+
Documentation comments use an extra slash or an extra star, like this:
5867

5968
```rust
6069
/// The foo function doesn’t really do much.
@@ -67,12 +76,21 @@ fn foo() {
6776
/// like we did before.
6877
fn bar() {
6978

79+
}
80+
81+
/**
82+
Multiple line documentation comments
83+
can also use this style.
84+
*/
85+
fn baz() {
86+
7087
}
7188
```
7289

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
7491
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.
7694

7795
Because documentation comments have semantic meaning to `rustdoc`, the compiler
7896
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
94112
whatever code comes directly after it, so it sees that a document comment alone
95113
must be a mistake.
96114

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

Comments
 (0)