Skip to content

Commit b7a4bd9

Browse files
committed
introducing writing documentation
added front page sample component documentation Added reference and include chapters
1 parent 09c4204 commit b7a4bd9

File tree

5 files changed

+269
-22
lines changed

5 files changed

+269
-22
lines changed

src/doc/rustdoc/src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
- [What is rustdoc?](what-is-rustdoc.md)
44
- [How to write documentation](how-to-write-documentation.md)
5+
- [What to include (and exclude)](what-to-include.md)
56
- [Command-line arguments](command-line-arguments.md)
67
- [The `#[doc]` attribute](the-doc-attribute.md)
78
- [Documentation tests](documentation-tests.md)
89
- [Lints](lints.md)
910
- [Passes](passes.md)
1011
- [Advanced Features](advanced-features.md)
1112
- [Unstable features](unstable-features.md)
13+
- [References](references.md)

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

Lines changed: 101 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,85 @@
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.
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+
312
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
714
as you can, and as complete as possible. As a rule of thumb: the more
815
documentation you write for your crate the better. If an item is public
916
then it should be documented.
1017

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

1384
It is recommended that each item's documentation follows this basic structure:
1485

@@ -23,9 +94,9 @@ It is recommended that each item's documentation follows this basic structure:
2394
```
2495

2596
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.
29100

30101
Let's see an example coming from the [standard library] by taking a look at the
31102
[`std::env::args()`][env::args] function:
@@ -62,6 +133,19 @@ for argument in env::args() {
62133
[`args_os`]: ./fn.args_os.html
63134
``````
64135

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+
65149
As you can see, it follows the structure detailed above: it starts with a short
66150
sentence explaining what the functions does, then it provides more information
67151
and finally provides a code example.
@@ -71,12 +155,16 @@ and finally provides a code example.
71155
`rustdoc` is using the [commonmark markdown specification]. You might be
72156
interested into taking a look at their website to see what's possible to do.
73157

74-
## Lints
158+
[commonmark quick reference] is a very helpful resource for the majority of
159+
use cases.
75160

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].
78161

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/
81163
[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

src/doc/rustdoc/src/references.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# References
2+
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]
8+
9+
## Official
10+
11+
- [Learn Rust]
12+
- [Rust By Example]
13+
- [Rust Reference]
14+
- [RFC 1574: More API Documentation Conventions]
15+
- [RFC 1946: Intra Rustdoc Links]
16+
17+
## Community
18+
- [API Guidelines]
19+
- [Github tagged RFCs]
20+
- [Github tagged issues]
21+
- [RFC (stalled) front page styleguide]
22+
- [Guide on how to write documenation for a Rust crate]
23+
24+
25+
[API Guidelines]: https://rust-lang.github.io/api-guidelines/documentation.html
26+
[Github tagged RFCs]: https://github.com/rust-lang/rfcs/issues?q=label%3AT-rustdoc
27+
[Github tagged issues]: https://github.com/rust-lang/rust/issues?q=is%3Aissue+is%3Aopen+label%3AT-rustdoc
28+
[Guide on how to write documenation for a Rust crate]: https://blog.guillaume-gomez.fr/articles/2020-03-12+Guide+on+how+to+write+documentation+for+a+Rust+crate
29+
[Learn Rust]: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-comments
30+
[RFC 1574: More API Documentation Conventions]: https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html
31+
[RFC 1946: Intra Rustdoc Links]: https://rust-lang.github.io/rfcs/1946-intra-rustdoc-links.html
32+
[RFC (stalled) front page styleguide]: https://github.com/rust-lang/rfcs/pull/1687
33+
[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
35+
[Rust Reference]: https://doc.rust-lang.org/stable/reference/comments.html#doc-comments

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ $ rustdoc src/lib.rs
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
3434
a web browser, you'll see a page with a search bar, and "Crate lib" at the
35-
top, with no contents. There's two problems with this: first, why does it
35+
top, with no contents.
36+
37+
## Configuring rustdoc
38+
39+
There's two problems with this: first, why does it
3640
think that our package is named "lib"? Second, why does it not have any
3741
contents?
3842

@@ -85,13 +89,12 @@ dependency=<path>/docs/target/debug/deps
8589
You can see this with `cargo doc --verbose`.
8690

8791
It generates the correct `--crate-name` for us, as well as pointing to
88-
`src/lib.rs` But what about those other arguments? `-o` controls the
89-
*o*utput of our docs. Instead of a top-level `doc` directory, you'll
90-
notice that Cargo puts generated documentation under `target`. That's
91-
the idiomatic place for generated files in Cargo projects. Also, it
92-
passes `-L`, a flag that helps rustdoc find the dependencies
93-
your code relies on. If our project used dependencies, we'd get
94-
documentation for them as well!
92+
`src/lib.rs` But what about those other arguments?
93+
- `-o` controls the *o*utput of our docs. Instead of a top-level
94+
`doc` directory, notice that Cargo puts generated documentation under
95+
`target`. That is the idiomatic place for generated files in Cargo projects.
96+
- `-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!
9598

9699
## Using standalone Markdown files
97100

@@ -105,7 +108,7 @@ This is a project to test out `rustdoc`.
105108
106109
[Here is a link!](https://www.rust-lang.org)
107110
108-
## Subheading
111+
## Example
109112
110113
```rust
111114
fn foo() -> i32 {
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# What to include (and exclude)
2+
3+
It is easy to say everything must be documented in a project and often times
4+
that is correct, but how can we get there, and are there things that don't
5+
belong?
6+
7+
At the top of the `src/lib.rs` or `main.rs` file in your binary project, include
8+
the following attribute:
9+
10+
```rust
11+
#![warn(missing_docs)]
12+
```
13+
14+
Now run `cargo doc` and examine the output. Here's a sample:
15+
16+
```text
17+
Documenting docdemo v0.1.0 (/Users/username/docdemo)
18+
warning: missing documentation for the crate
19+
--> src/main.rs:1:1
20+
|
21+
1 | / #![warn(missing_docs)]
22+
2 | |
23+
3 | | fn main() {
24+
4 | | println!("Hello, world!");
25+
5 | | }
26+
| |_^
27+
|
28+
note: the lint level is defined here
29+
--> src/main.rs:1:9
30+
|
31+
1 | #![warn(missing_docs)]
32+
| ^^^^^^^^^^^^
33+
34+
warning: 1 warning emitted
35+
36+
Finished dev [unoptimized + debuginfo] target(s) in 2.96s
37+
```
38+
39+
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.
42+
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+
```
64+
There are more lints in the upcoming chapter [Lints][rustdoc-lints].
65+
66+
## Examples
67+
68+
Of course this is contrived to be simple, but part of the power of documentation
69+
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.
72+
73+
`Async` is a good example of this. In order to execute an `async` example, an
74+
executor needs to be available. Examples will often shortcut this, and leave
75+
users to figure out how to put the `async` code into their own runtime.
76+
77+
It is preferred that `unwrap()` not be used inside an example, and some of the
78+
error handling components be hidden if they make the example too difficult to
79+
follow.
80+
81+
``````rust
82+
/// Example
83+
/// ```rust
84+
/// let fourtytwo = "42".parse::<u32>()?;
85+
/// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
86+
/// ```
87+
``````
88+
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:
92+
93+
``````rust
94+
/// Example
95+
/// ```rust
96+
/// # main() -> Result<(), std::num::ParseIntError> {
97+
/// let fourtytwo = "42".parse::<u32>()?;
98+
/// println!("{} + 10 = {}", fourtytwo, fourtytwo+10);
99+
/// # Ok(())
100+
/// # }
101+
/// ```
102+
``````
103+
104+
The example is the same on the doc page, but has that extra information
105+
available to anyone trying to use your crate. More about tests in the
106+
upcoming [Documentation tests] chapter.
107+
108+
## What to Exclude
109+
110+
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.
113+
114+
Good examples are `impl From<Error>` for providing `?` usage, or `impl Display`.
115+
116+
117+
118+
[Documentation tests]: documentation-tests.md
119+
[rustdoc-lints]: lints.md

0 commit comments

Comments
 (0)