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.
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.
11
+
3
12
This chapter covers not only how to write documentation but specifically
4
- how to write ** good** documentation. Something to keep in mind when
5
- writing documentation is that your audience is not just yourself but others
6
- who simply don't have the context you do. It is important to be as clear
13
+ how to write ** good** documentation. It is important to be as clear
7
14
as you can, and as complete as possible. As a rule of thumb: the more
8
15
documentation you write for your crate the better. If an item is public
9
16
then it should be documented.
10
17
11
- ## Basic structure
18
+ ## Getting Started
19
+
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.
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.
30
+
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.
34
+
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.
38
+
39
+ 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
43
+ then concludes with crate features.
44
+
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!
48
+
49
+ The first lines within the ` lib.rs ` will compose the front-page, and they
50
+ 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:
53
+
54
+ ``` rust
55
+ // ! Fast and easy queue abstraction.
56
+ // !
57
+ // ! Provides an abstraction over a queue. When the abstraction is used
58
+ // ! there are these advantages:
59
+ // ! - Fast
60
+ // ! - [`Easy`]
61
+ // !
62
+ // ! [`Easy`]: http://thatwaseasy.example.com
63
+
64
+ /// This module makes it easy.
65
+ pub mod easy {
66
+
67
+ /// Use the abstract function to do this specific thing.
68
+ pub fn abstract {}
69
+
70
+ }
71
+ ```
72
+
73
+ 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.
77
+
78
+ ## Documenting components
79
+
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.
12
83
13
84
It is recommended that each item's documentation follows this basic structure:
14
85
@@ -23,9 +94,9 @@ It is recommended that each item's documentation follows this basic structure:
23
94
```
24
95
25
96
This basic structure should be straightforward to follow when writing your
26
- documentation and, while you might think that a code example is trivial,
27
- the examples are really important because they can help your users to
28
- understand what an item is, how it is used, and for what purpose it exists.
97
+ documentation; while you might think that a code example is trivial,
98
+ the examples are really important because they can help users understand
99
+ what an item is, how it is used, and for what purpose it exists.
29
100
30
101
Let's see an example coming from the [ standard library] by taking a look at the
31
102
[ ` std::env::args() ` ] [ env::args ] function:
@@ -62,6 +133,19 @@ for argument in env::args() {
62
133
[`args_os`]: ./fn.args_os.html
63
134
``````
64
135
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.
139
+
140
+ 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.
144
+
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
147
+ every time edge cases in your code can be reached if known.
148
+
65
149
As you can see, it follows the structure detailed above: it starts with a short
66
150
sentence explaining what the functions does, then it provides more information
67
151
and finally provides a code example.
@@ -71,12 +155,16 @@ and finally provides a code example.
71
155
` rustdoc ` is using the [ commonmark markdown specification] . You might be
72
156
interested into taking a look at their website to see what's possible to do.
73
157
74
- ## Lints
158
+ [ commonmark quick reference] is a very helpful resource for the majority of
159
+ use cases.
75
160
76
- To be sure that you didn't miss any item without documentation or code examples,
77
- you can take a look at the rustdoc lints [ here] [ rustdoc-lints ] .
78
161
79
- [ standard library ] : https://doc.rust-lang.org/stable/std/index.html
80
- [ env::args ] : https://doc.rust-lang.org/stable/std/env/fn.args.html
162
+ [ backtrace ] : https://docs.rs/backtrace/0.3.50/backtrace/
81
163
[ commonmark markdown specification ] : https://commonmark.org/
82
- [ rustdoc-lints ] : lints.md
164
+ [ commonmark quick reference ] : https://commonmark.org/help/
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/
169
+ [ standard library ] : https://doc.rust-lang.org/stable/std/index.html
170
+ [ `std::env` ] : https://doc.rust-lang.org/stable/std/env/index.html#functions
0 commit comments