1
1
# How to write documentation
2
2
3
- Good documentation is not natural. There are opposing forces that make good
4
- documentation difficult. It requires expertise in the subject but also
5
- requires writing to a novice perspective. Documentation therefore often
6
- glazes over implementation detail, or leaves an explain like I'm 5 response .
3
+ Good documentation is not natural. There are opposing goals that make writing
4
+ good documentation difficult. It requires expertise in the subject but also
5
+ writing to a novice perspective. Documentation therefore often glazes over
6
+ implementation detail, or leaves readers with unanswered questions .
7
7
8
- There are tenants to Rust documentation that can help guide anyone through
9
- the process of documenting libraries so everyone has ample opportunity to
10
- use the code.
8
+ There are a few tenets to Rust documentation that can help guide anyone through
9
+ the process of documenting libraries so that everyone has an ample opportunity
10
+ to use the code.
11
11
12
12
This chapter covers not only how to write documentation but specifically
13
13
how to write ** good** documentation. It is important to be as clear
@@ -18,38 +18,37 @@ then it should be documented.
18
18
## Getting Started
19
19
20
20
Documenting a crate should begin with front-page documentation. As an
21
- example, [ hashbrown] crate level documentation summarizes the role of
22
- the crate, provides links to explain technical details, and gives the
23
- reason why to use the crate.
21
+ example, the [ ` hashbrown ` ] crate level documentation summarizes the role of
22
+ the crate, provides links to explain technical details, and explains why you
23
+ would want to use the crate.
24
24
25
- After introducing the crate, it is important that within the front-page
26
- an example be given how to use the crate in a real world setting. The
27
- example benefits from isolating the library's role from the implementation
28
- details, but doing so without shortcuts also benefits users who may copy
29
- and paste the example to get started.
25
+ After introducing the crate, it is important that the front-page gives
26
+ an example of how to use the crate in a real world setting. Stick to the
27
+ library's role in the example, but do so without shortcuts to benefit users who
28
+ may copy and paste the example to get started.
30
29
31
- [ futures] uses an approach of inline comments to explain line by line
32
- the complexities of using a future , because often people 's first exposure to
33
- rust's future is this example.
30
+ [ ` futures ` ] uses inline comments to explain line by line
31
+ the complexities of using a [ ` Future ` ] , because a person 's first exposure to
32
+ rust's [ ` Future ` ] may be this example.
34
33
35
- [ backtrace] usage walks through the whole process, explaining changes made
36
- to the ` Cargo.toml ` file, passing command line arguments to the compiler,
37
- and shows a quick example of backtrace in the wild.
34
+ The [ ` backtrace ` ] documentation walks through the whole process, explaining
35
+ changes made to the ` Cargo.toml ` file, passing command line arguments to the
36
+ compiler, and shows a quick example of backtrace in the wild.
38
37
39
38
Finally, the front-page can eventually become a comprehensive reference
40
- how to ues a crate, like the usage found in [ regex] . In this front page, all
41
- the requirements are outlined, the gotchas are taught, then practical examples
42
- are provided. The front page goes on to show how to use regular expressions
39
+ how to use a crate, like [ ` regex ` ] . In this front page, all
40
+ requirements are outlined, the edge cases shown, and practical examples
41
+ provided. The front page goes on to show how to use regular expressions
43
42
then concludes with crate features.
44
43
45
- Don't worry about comparing your crate that is just beginning to get
46
- documentation to something more polished, just start incrementally and put
47
- in an introduction, example, and features. Rome wasn't built in a day!
44
+ Don't worry about comparing your crate, which is just beginning. To get the
45
+ documentation to something more polished, start incrementally and put
46
+ in an introduction, example, and features. Rome was not built in a day!
48
47
49
48
The first lines within the ` lib.rs ` will compose the front-page, and they
50
49
use a different convention than the rest of the rustdocs. Lines should
51
- start with ` //! ` which designate the code to refer to module-level or crate-
52
- level documentation. Here's a quick example of the difference:
50
+ start with ` //! ` which indicate module-level or crate-level documentation.
51
+ Here's a quick example of the difference:
53
52
54
53
``` rust
55
54
// ! Fast and easy queue abstraction.
@@ -65,21 +64,21 @@ level documentation. Here's a quick example of the difference:
65
64
pub mod easy {
66
65
67
66
/// Use the abstract function to do this specific thing.
68
- pub fn abstract {}
67
+ pub fn abstract () {}
69
68
70
69
}
71
70
```
72
71
73
72
Ideally, this first line of documentation is a sentence without highly
74
- technical details, but very broadly descriptive of where this crate fits
75
- within the rust ecosystem. Someone should know if this crate is qualified
76
- for investigation in their use case by this line.
73
+ technical details, but descriptive of where this crate fits
74
+ within the rust ecosystem. Users should know whether this crate meets their use
75
+ case after reading this line.
77
76
78
77
## Documenting components
79
78
80
- Whether documenting modules, structs, functions, or macros, the public
81
- API of all code should have some documentation, and rarely does anyone
82
- complain about too much documentation.
79
+ Whether it is modules, structs, functions, or macros: the public
80
+ API of all code should have documentation. Rarely does anyone
81
+ complain about too much documentation!
83
82
84
83
It is recommended that each item's documentation follows this basic structure:
85
84
@@ -98,7 +97,7 @@ documentation; while you might think that a code example is trivial,
98
97
the examples are really important because they can help users understand
99
98
what an item is, how it is used, and for what purpose it exists.
100
99
101
- Let's see an example coming from the [ standard library] by taking a look at the
100
+ Let us see an example coming from the [ standard library] by taking a look at the
102
101
[ ` std::env::args() ` ] [ env::args ] function:
103
102
104
103
`````` text
@@ -133,17 +132,19 @@ for argument in env::args() {
133
132
[`args_os`]: ./fn.args_os.html
134
133
``````
135
134
136
- The first line of description will be reused when describing the component in
137
- a higher level of the documentation. For example, the function ` std::env::args() `
138
- above can be found within the [ ` std::env ` ] module documentation.
135
+ The first line of description will be reused to describe the component in
136
+ searches and module overviews. For example, the function ` std::env::args() `
137
+ above will be shown on the [ ` std::env ` ] module documentation. Multi-line
138
+ summaries are also possible, however, concise writing is a goal of good
139
+ documentation.
139
140
140
141
Because the type system does a good job of defining what is passed to a function
141
- and what is returned from one, there is not a benefit of explicitly writing those
142
- things into the documentation. Rustdoc makes sure the links to the types included
143
- in the signature are linked.
142
+ and what is returned from one, there is not a benefit of explicitly writing it
143
+ into the documentation. Rustdoc makes sure the links to the types included
144
+ in the function signature are linked.
144
145
145
- In the example above, a Panics section explains when the code might abruptly exit
146
- which can help the reader build guards if required . A panic section is recommended
146
+ In the example above, a ' Panics' section explains when the code might abruptly exit,
147
+ which can help the reader prevent reaching a panic . A panic section is recommended
147
148
every time edge cases in your code can be reached if known.
148
149
149
150
As you can see, it follows the structure detailed above: it starts with a short
@@ -154,17 +155,18 @@ and finally provides a code example.
154
155
155
156
` rustdoc ` is using the [ commonmark markdown specification] . You might be
156
157
interested into taking a look at their website to see what's possible to do.
157
-
158
- [ commonmark quick reference] is a very helpful resource for the majority of
159
- use cases.
158
+ - [ commonmark quick reference]
159
+ - [ current spec]
160
160
161
161
162
162
[ backtrace ] : https://docs.rs/backtrace/0.3.50/backtrace/
163
163
[ commonmark markdown specification ] : https://commonmark.org/
164
164
[ commonmark quick reference ] : https://commonmark.org/help/
165
165
[ env::args ] : https://doc.rust-lang.org/stable/std/env/fn.args.html
166
- [ futures ] : https://docs.rs/futures/0.3.5/futures/
167
- [ hashbrown ] : https://docs.rs/hashbrown/0.8.2/hashbrown/
168
- [ regex ] : https://docs.rs/regex/1.3.9/regex/
166
+ [ ` Future ` ] : https://doc.rust-lang.org/std/future/trait.Future.html
167
+ [ `futures` ] : https://docs.rs/futures/0.3.5/futures/
168
+ [ `hashbrown` ] : https://docs.rs/hashbrown/0.8.2/hashbrown/
169
+ [ `regex` ] : https://docs.rs/regex/1.3.9/regex/
169
170
[ standard library ] : https://doc.rust-lang.org/stable/std/index.html
171
+ [ current spec ] : https://spec.commonmark.org/current/
170
172
[ `std::env` ] : https://doc.rust-lang.org/stable/std/env/index.html#functions
0 commit comments