Skip to content

Commit cde5637

Browse files
authored
Rollup merge of rust-lang#66267 - GuillaumeGomez:add-rustdoc-doc, r=kinnison
Add rustdoc doc r? @kinnison
2 parents 896484c + 528b059 commit cde5637

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

src/doc/rustdoc/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# The Rustdoc Book
22

33
- [What is rustdoc?](what-is-rustdoc.md)
4+
- [How to write documentation](how-to-write-documentation.md)
45
- [Command-line arguments](command-line-arguments.md)
56
- [The `#[doc]` attribute](the-doc-attribute.md)
67
- [Documentation tests](documentation-tests.md)
8+
- [Lints](lints.md)
79
- [Passes](passes.md)
810
- [Unstable features](unstable-features.md)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# How to write documentation
2+
3+
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
7+
as you can, and as complete as possible. As a rule of thumb: the more
8+
documentation you write for your crate the better. If an item is public
9+
then it should be documented.
10+
11+
## Basic structure
12+
13+
It is recommended that each item's documentation follows this basic structure:
14+
15+
```text
16+
[short sentence explaining what it is]
17+
18+
[more detailed explanation]
19+
20+
[at least one code example that users can copy/paste to try it]
21+
22+
[even more advanced explanations if necessary]
23+
```
24+
25+
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.
29+
30+
Let's see an example coming from the [standard library] by taking a look at the
31+
[`std::env::args()`][env::args] function:
32+
33+
``````text
34+
Returns the arguments which this program was started with (normally passed
35+
via the command line).
36+
37+
The first element is traditionally the path of the executable, but it can be
38+
set to arbitrary text, and may not even exist. This means this property should
39+
not be relied upon for security purposes.
40+
41+
On Unix systems shell usually expands unquoted arguments with glob patterns
42+
(such as `*` and `?`). On Windows this is not done, and such arguments are
43+
passed as-is.
44+
45+
# Panics
46+
47+
The returned iterator will panic during iteration if any argument to the
48+
process is not valid unicode. If this is not desired,
49+
use the [`args_os`] function instead.
50+
51+
# Examples
52+
53+
```
54+
use std::env;
55+
56+
// Prints each argument on a separate line
57+
for argument in env::args() {
58+
println!("{}", argument);
59+
}
60+
```
61+
62+
[`args_os`]: ./fn.args_os.html
63+
``````
64+
65+
As you can see, it follows the structure detailed above: it starts with a short
66+
sentence explaining what the functions does, then it provides more information
67+
and finally provides a code example.
68+
69+
## Markdown
70+
71+
`rustdoc` is using the [commonmark markdown specification]. You might be
72+
interested into taking a look at their website to see what's possible to do.
73+
74+
## Lints
75+
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+
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
81+
[commonmark markdown specification]: https://commonmark.org/
82+
[rustdoc-lints]: lints.md

src/doc/rustdoc/src/lints.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Lints
2+
3+
`rustdoc` provides lints to help you writing and testing your documentation. You
4+
can use them like any other lints by doing this:
5+
6+
```rust,ignore
7+
#![allow(missing_docs)] // allowing the lint, no message
8+
#![warn(missing_docs)] // warn if there is missing docs
9+
#![deny(missing_docs)] // rustdoc will fail if there is missing docs
10+
```
11+
12+
Here is the list of the lints provided by `rustdoc`:
13+
14+
## intra_doc_link_resolution_failure
15+
16+
This lint **warns by default** and is **nightly-only**. This lint detects when
17+
an intra-doc link fails to get resolved. For example:
18+
19+
```rust
20+
/// I want to link to [`Inexistent`] but it doesn't exist!
21+
pub fn foo() {}
22+
```
23+
24+
You'll get a warning saying:
25+
26+
```text
27+
error: `[`Inexistent`]` cannot be resolved, ignoring it...
28+
```
29+
30+
## missing_docs
31+
32+
This lint is **allowed by default**. It detects items missing documentation.
33+
For example:
34+
35+
```rust
36+
#![warn(missing_docs)]
37+
38+
pub fn undocumented() {}
39+
# fn main() {}
40+
```
41+
42+
The `undocumented` function will then have the following warning:
43+
44+
```text
45+
warning: missing documentation for a function
46+
--> your-crate/lib.rs:3:1
47+
|
48+
3 | pub fn undocumented() {}
49+
| ^^^^^^^^^^^^^^^^^^^^^
50+
```
51+
52+
## missing_doc_code_examples
53+
54+
This lint is **allowed by default**. It detects when a documentation block
55+
is missing a code example. For example:
56+
57+
```rust
58+
#![warn(missing_doc_code_examples)]
59+
60+
/// There is no code example!
61+
pub fn no_code_example() {}
62+
# fn main() {}
63+
```
64+
65+
The `no_code_example` function will then have the following warning:
66+
67+
```text
68+
warning: Missing code example in this documentation
69+
--> your-crate/lib.rs:3:1
70+
|
71+
LL | /// There is no code example!
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
```
74+
75+
To fix the lint, you need to add a code example into the documentation block:
76+
77+
```rust
78+
/// There is no code example!
79+
///
80+
/// ```
81+
/// println!("calling no_code_example...");
82+
/// no_code_example();
83+
/// println!("we called no_code_example!");
84+
/// ```
85+
pub fn no_code_example() {}
86+
```
87+
88+
## private_doc_tests
89+
90+
This lint is **allowed by default**. It detects documentation tests when they
91+
are on a private item. For example:
92+
93+
```rust
94+
#![warn(private_doc_tests)]
95+
96+
mod foo {
97+
/// private doc test
98+
///
99+
/// ```
100+
/// assert!(false);
101+
/// ```
102+
fn bar() {}
103+
}
104+
# fn main() {}
105+
```
106+
107+
Which will give:
108+
109+
```text
110+
warning: Documentation test in private item
111+
--> your-crate/lib.rs:4:1
112+
|
113+
4 | / /// private doc test
114+
5 | | ///
115+
6 | | /// ```
116+
7 | | /// assert!(false);
117+
8 | | /// ```
118+
| |___________^
119+
```

0 commit comments

Comments
 (0)