Skip to content

Commit 34c15c3

Browse files
Add explanations on how to write documentation for std/core
1 parent 4f10401 commit 34c15c3

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
- [Documentation](./documentation/summary.md)
4949
- [doc alias policy](./documentation/doc-alias-policy.md)
5050
- [safety comments policy](./documentation/safety-comments.md)
51+
- [how to write documentation](./how-to-write-documentation.md)
5152

5253
---
5354

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# How to write documentation
2+
3+
This document explains how to write documentation for the std/core public APIs.
4+
5+
Let's start with some general information:
6+
7+
### Contractions
8+
9+
It's common in english to have contractions such "don't" or "can't". They can
10+
never be used in documentation. Always write their "full form":
11+
* "do not" instead of "don't"
12+
* "cannot" instead of "can't"
13+
* "it would" instead of "it'd"
14+
* "it'll" instead of "it'll"
15+
* etc
16+
17+
The reason is simply to make the reading simpler as many readers are not english
18+
native or could have reading issues.
19+
20+
### When to use inline code blocks
21+
22+
Whenever you're talking about a type or anything code related, it should be in a
23+
inline code block. As a reminder, a inline code block is created with backticks
24+
(\`). For example:
25+
26+
27+
```text
28+
This a `Vec` and it has a method `push` which you can call by doing `Vec::push`.
29+
```
30+
31+
### When to use intra-doc links
32+
33+
Intra-doc links (you can see the full explanations for the feature
34+
[here](https://doc.rust-lang.org/rustdoc/write-documentation/linking-to-items-by-name.html))
35+
should be used as much as possible whenever a type is mentioned.
36+
37+
Little note: when you're documenting an item, no need to link to it. So if you
38+
write documentation for `String::push_str` method, no need to link to the method
39+
`push_str` or to the `String` type.
40+
41+
If you have cases like `Vec<String>`, you need to use intra-doc links on both
42+
`Vec` and `String` as well. It would look like this:
43+
44+
```text
45+
This is a [`Vec`]`<`[`String`]`>`.
46+
```
47+
48+
Extra explanations: since both `Vec` and `String` are in codeblocks, `<` and `>`
49+
should as well, otherwise it would render badly.
50+
51+
### Code blocks
52+
53+
With rustdoc, code blocks are tested (because considered as Rust code blocks by
54+
default). It allows us to know if the documentation is up to date. As such,
55+
please avoid to use `ignore` as much as possible on code blocks! If you want to
56+
write into another language than Rust, simply set it in the code block tags:
57+
58+
```text
59+
This is not rust code!
60+
```
61+
62+
Some special cases:
63+
* If the code example cannot be run (when documenting a I/O item for example),
64+
use `no_run`.
65+
* If it's expected to fail, use `should_panic`.
66+
* If it's expected to fail compilation (which be quite rare!), use `compile_fail`.
67+
68+
You can find more information about code blocks
69+
[here](https://doc.rust-lang.org/rustdoc/write-documentation/documentation-tests.html).
70+
71+
## How to write documentation for a module
72+
73+
A module is supposed to contain "similar" items. As such, its documentation is
74+
supposed to give an overview and eventually **a base** to understand what the
75+
items it contains are doing.
76+
77+
You can take a look at the
78+
[f32 module](https://doc.rust-lang.org/nightly/std/f32/index.html) or at the
79+
[fmt module](https://doc.rust-lang.org/nightly/std/fmt/index.html) to see
80+
both cases.
81+
82+
## How to write documentation for functions/methods
83+
84+
The basic format of each documented methods/functions should roughly look like this:
85+
86+
```text
87+
[explanations]
88+
89+
[example(s)]
90+
```
91+
92+
### Explanations
93+
94+
By `explanations` we mean that the text should explain what the method and what
95+
each of its arguments are for. Let's take this method for example:
96+
97+
```rust
98+
pub fn concat_str(&self, s: &str) -> String {
99+
if s.is_empty() {
100+
panic!("empty concat string");
101+
}
102+
format!("{}{}", self.string, s)
103+
}
104+
```
105+
106+
The explanation should look like this:
107+
108+
```text
109+
Returns a new [`String`] which contains `&self` content with `s` added at the end.
110+
```
111+
112+
### Panic?
113+
114+
If the function/method can panic in certain circumstances, it must to be
115+
mentioned! This explanation needs to be prepended by a `Panics` title:
116+
117+
```text
118+
# Panics
119+
120+
`concat_str` panics if `s` is empty.
121+
```
122+
123+
### Examples
124+
125+
As for the examples, they have to show the usage of the function/method. Just
126+
like the `panic` section, they need to be prepended by a `Examples` title.
127+
128+
It's better if you use `assert*!` macros at the end to ensure that the example
129+
is working as expected. It also allows the readers to understand more easily
130+
what the function is doing (or returning).
131+
132+
# Examples
133+
134+
```
135+
let s = MyType::new("hello ");
136+
assert_eq!("hello Georges", s.concat_str("Georges").as_str());
137+
```
138+
139+
## How to write documentation for other items
140+
141+
It's mostly the same as for methods and functions except that the examples
142+
are (strongly) recommended and not mandatory.
143+
144+
A good example often shows how to create the item.

src/documentation/summary.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- [doc alias policy](./doc-alias-policy.md)
22
- [safety comments policy](./safety-comments.md)
3+
- [how to write documentation](./how-to-write-documentation.md)

0 commit comments

Comments
 (0)