Skip to content

Commit 0f605b0

Browse files
committed
Rust Doc book improvements post-review
1 parent b7a4bd9 commit 0f605b0

File tree

5 files changed

+106
-102
lines changed

5 files changed

+106
-102
lines changed

src/doc/rustdoc/src/advanced-features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ pub struct UnixToken;
3131
```
3232

3333
Here, the respective tokens can only be used by dependent crates on their respective platforms, but
34-
they will both appear in documentation.
34+
they will both appear in documentation.

src/doc/rustdoc/src/how-to-write-documentation.md

+53-51
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# How to write documentation
22

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.
77

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.
1111

1212
This chapter covers not only how to write documentation but specifically
1313
how to write **good** documentation. It is important to be as clear
@@ -18,38 +18,37 @@ then it should be documented.
1818
## Getting Started
1919

2020
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.
2424

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.
3029

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.
3433

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.
3837

3938
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
4342
then concludes with crate features.
4443

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!
4847

4948
The first lines within the `lib.rs` will compose the front-page, and they
5049
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:
5352

5453
```rust
5554
//! Fast and easy queue abstraction.
@@ -65,21 +64,21 @@ level documentation. Here's a quick example of the difference:
6564
pub mod easy {
6665

6766
/// Use the abstract function to do this specific thing.
68-
pub fn abstract {}
67+
pub fn abstract() {}
6968

7069
}
7170
```
7271

7372
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.
7776

7877
## Documenting components
7978

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!
8382

8483
It is recommended that each item's documentation follows this basic structure:
8584

@@ -98,7 +97,7 @@ documentation; while you might think that a code example is trivial,
9897
the examples are really important because they can help users understand
9998
what an item is, how it is used, and for what purpose it exists.
10099

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
102101
[`std::env::args()`][env::args] function:
103102

104103
``````text
@@ -133,17 +132,19 @@ for argument in env::args() {
133132
[`args_os`]: ./fn.args_os.html
134133
``````
135134

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.
139140

140141
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.
144145

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
147148
every time edge cases in your code can be reached if known.
148149

149150
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.
154155

155156
`rustdoc` is using the [commonmark markdown specification]. You might be
156157
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]
160160

161161

162162
[backtrace]: https://docs.rs/backtrace/0.3.50/backtrace/
163163
[commonmark markdown specification]: https://commonmark.org/
164164
[commonmark quick reference]: https://commonmark.org/help/
165165
[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/
169170
[standard library]: https://doc.rust-lang.org/stable/std/index.html
171+
[current spec]: https://spec.commonmark.org/current/
170172
[`std::env`]: https://doc.rust-lang.org/stable/std/env/index.html#functions

src/doc/rustdoc/src/references.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
# References
22

3-
There are many great `rustdoc` references out there. This is a collection of
4-
those documentation documents on the internet at the time of writing. If you
5-
know of other great resources, please submit a pull request:
6-
7-
[Rust official Github]
3+
There are many great `rustdoc` references out there.
4+
If you know of other great resources, please submit a pull request
85

96
## Official
107

@@ -31,5 +28,4 @@ know of other great resources, please submit a pull request:
3128
[RFC 1946: Intra Rustdoc Links]: https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html
3229
[RFC (stalled) front page styleguide]: https://github.com/rust-lang/rfcs/pull/1687
3330
[Rust By Example]: https://doc.rust-lang.org/stable/rust-by-example/meta/doc.html
34-
[Rust official Github]: https://github.com/rust-lang/rust/pulls
3531
[Rust Reference]: https://doc.rust-lang.org/stable/reference/comments.html#doc-comments

src/doc/rustdoc/src/what-is-rustdoc.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ CSS, and JavaScript.
77

88
## Basic usage
99

10-
Let's give it a try! Let's create a new project with Cargo:
10+
Let us give it a try! Create a new project with Cargo:
1111

1212
```bash
1313
$ cargo new docs
1414
$ cd docs
1515
```
1616

17-
In `src/lib.rs`, you'll find that Cargo has generated some sample code. Delete
17+
In `src/lib.rs`, Cargo has generated some sample code. Delete
1818
it and replace it with this:
1919

2020
```rust
2121
/// foo is a function
2222
fn foo() {}
2323
```
2424

25-
Let's run `rustdoc` on our code. To do so, we can call it with the path to
25+
Now, run `rustdoc` on our code. To do so, we can call it with the path to
2626
our crate root like this:
2727

2828
```bash
@@ -31,12 +31,12 @@ $ rustdoc src/lib.rs
3131

3232
This will create a new directory, `doc`, with a website inside! In our case,
3333
the main page is located in `doc/lib/index.html`. If you open that up in
34-
a web browser, you'll see a page with a search bar, and "Crate lib" at the
34+
a web browser, you will see a page with a search bar, and "Crate lib" at the
3535
top, with no contents.
3636

3737
## Configuring rustdoc
3838

39-
There's two problems with this: first, why does it
39+
There are two problems with this: first, why does it
4040
think that our package is named "lib"? Second, why does it not have any
4141
contents?
4242

@@ -50,7 +50,7 @@ $ rustdoc src/lib.rs --crate-name docs
5050

5151
Now, `doc/docs/index.html` will be generated, and the page says "Crate docs."
5252

53-
For the second issue, it's because our function `foo` is not public; `rustdoc`
53+
For the second issue, it is because our function `foo` is not public; `rustdoc`
5454
defaults to generating documentation for only public functions. If we change
5555
our code...
5656

@@ -65,7 +65,7 @@ pub fn foo() {}
6565
$ rustdoc src/lib.rs --crate-name docs
6666
```
6767

68-
We'll have some generated documentation. Open up `doc/docs/index.html` and
68+
We will have some generated documentation. Open up `doc/docs/index.html` and
6969
check it out! It should show a link to the `foo` function's page, which
7070
is located at `doc/docs/fn.foo.html`. On that page, you'll see the "foo is
7171
a function" we put inside the documentation comment in our crate.
@@ -89,16 +89,16 @@ dependency=<path>/docs/target/debug/deps
8989
You can see this with `cargo doc --verbose`.
9090

9191
It generates the correct `--crate-name` for us, as well as pointing to
92-
`src/lib.rs` But what about those other arguments?
92+
`src/lib.rs`. But what about those other arguments?
9393
- `-o` controls the *o*utput of our docs. Instead of a top-level
9494
`doc` directory, notice that Cargo puts generated documentation under
9595
`target`. That is the idiomatic place for generated files in Cargo projects.
9696
- `-L` flag helps rustdoc find the dependencies your code relies on.
97-
If our project used dependencies, we'd get documentation for them as well!
97+
If our project used dependencies, we would get documentation for them as well!
9898

9999
## Using standalone Markdown files
100100

101-
`rustdoc` can also generate HTML from standalone Markdown files. Let's
101+
`rustdoc` can also generate HTML from standalone Markdown files. Let us
102102
give it a try: create a `README.md` file with these contents:
103103

104104
````text
@@ -123,7 +123,7 @@ And call `rustdoc` on it:
123123
$ rustdoc README.md
124124
```
125125

126-
You'll find an HTML file in `docs/doc/README.html` generated from its
126+
You will find an HTML file in `docs/doc/README.html` generated from its
127127
Markdown contents.
128128

129129
Cargo currently does not understand standalone Markdown files, unfortunately.

src/doc/rustdoc/src/what-to-include.md

+39-33
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,20 @@ warning: 1 warning emitted
3737
```
3838

3939
As a library author, adding the lint `#![deny(missing_docs)]` is a great way to
40-
ensure the project does not drift away from being documented well, and warn is
41-
a good way to move towards comprehensive documentation.
40+
ensure the project does not drift away from being documented well, and
41+
`#![warn(missing_docs)]` is a good way to move towards comprehensive
42+
documentation. In addition to docs, `#![deny(missing_doc_code_examples)]`
43+
ensures each function contains a usage example. In our example above, the
44+
warning is resolved by adding crate level documentation.
4245

43-
In our example above, the warning is resolved by adding crate level
44-
documentation. The `main` method does not need documentation, but it is
45-
present in the output. If your main method is doing anything special, like
46-
returning a `Result`, it could use documentation!
47-
48-
```rust
49-
#![warn(missing_docs)]
50-
51-
//! Run program to print Hello, world.
52-
53-
/// Allows unhandled errors to become program return values
54-
fn main() -> std::io::Result<()> {
55-
print_it();
56-
Ok(())
57-
}
58-
59-
/// Safely prints Hello, world!
60-
fn print_it() {
61-
println!("Hello, world!");
62-
}
63-
```
6446
There are more lints in the upcoming chapter [Lints][rustdoc-lints].
6547

6648
## Examples
6749

6850
Of course this is contrived to be simple, but part of the power of documentation
6951
is showing code that is easy to follow, rather than being realistic. Docs often
70-
shortcut error handling because often examples become complicated to follow
71-
with all the necessary set up required for a simple example.
52+
take shortcuts with error handling because examples can become complicated to
53+
follow with all the necessary set up required for a simple example.
7254

7355
`Async` is a good example of this. In order to execute an `async` example, an
7456
executor needs to be available. Examples will often shortcut this, and leave
@@ -86,16 +68,16 @@ follow.
8668
/// ```
8769
``````
8870

89-
When rustdoc wraps that in a main function, it will panic due to the
90-
`ParseIntError` trait not implemented. In order to help both your audience
91-
and your test suite, this example needs to have additional work performed:
71+
When rustdoc wraps that in a main function, it will fail to compile because the
72+
`ParseIntError` trait is not implemented. In order to help both your audience
73+
and your test suite, this example needs some additional code:
9274

9375
``````rust
9476
/// Example
9577
/// ```rust
9678
/// # main() -> Result<(), std::num::ParseIntError> {
97-
/// let fourtytwo = "42".parse::<u32>()?;
98-
/// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
79+
/// let fortytwo = "42".parse::<u32>()?;
80+
/// println!("{} + 10 = {}", fortytwo, fortytwo+10);
9981
/// # Ok(())
10082
/// # }
10183
/// ```
@@ -108,12 +90,36 @@ upcoming [Documentation tests] chapter.
10890
## What to Exclude
10991

11092
Certain parts of your public interface may be included by default in the output
111-
of rustdoc. The attribute `#[doc(hidden)]` hides the non-public consumption
112-
implementation details to facilitate idiomatic Rust.
93+
of rustdoc. The attribute `#[doc(hidden)]` can hide implementation details
94+
to encourage idiomatic use of the crate.
95+
96+
For example, an internal `macro!` that makes the crate easier to implement can
97+
become a footgun for users when it appears in the public documentation. An
98+
internal `Error` type may exist, and `impl` details should be hidden, as
99+
detailed in the [API Guidelines].
100+
101+
## Customizing the output
113102

114-
Good examples are `impl From<Error>` for providing `?` usage, or `impl Display`.
103+
It is possible to pass a custom css file to `rustdoc` and style the
104+
documentation.
115105

106+
```bash
107+
rustdoc --extend-css custom.css src/lib.rs
108+
```
109+
110+
A good example of using this feature to create a dark theme is documented [on
111+
this blog]. Just remember, dark theme is already included in the rustdoc output
112+
by clicking on the paintbrush. Adding additional options to the themes are
113+
as easy as creating a custom theme `.css` file and using the following syntax:
114+
115+
```bash
116+
rustdoc --theme awesome.css src/lib.rs
117+
```
116118

119+
Here is an example of a new theme, [Ayu].
117120

121+
[Ayu]: https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/static/themes/ayu.css
122+
[API Guidelines]: https://rust-lang.github.io/api-guidelines/documentation.html#rustdoc-does-not-show-unhelpful-implementation-details-c-hidden
118123
[Documentation tests]: documentation-tests.md
124+
[on this blog]: https://blog.guillaume-gomez.fr/articles/2016-09-16+Generating+doc+with+rustdoc+and+a+custom+theme
119125
[rustdoc-lints]: lints.md

0 commit comments

Comments
 (0)