Skip to content

Commit

Permalink
chore: update english doc
Browse files Browse the repository at this point in the history
  • Loading branch information
wendajiang committed Jan 18, 2022
1 parent 3532f27 commit 466b14e
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 101 deletions.
1 change: 0 additions & 1 deletion english/book.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[book]
authors = ["The Rust Project Developers"]
src = "src"
title = "The rustdoc book"

Expand Down
2 changes: 2 additions & 0 deletions english/src/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# The Rustdoc Book

- [What is rustdoc?](what-is-rustdoc.md)
- [How to read rustdoc output](how-to-read-rustdoc.md)
- [How to write documentation](how-to-write-documentation.md)
- [What to include (and exclude)](what-to-include.md)
- [Command-line arguments](command-line-arguments.md)
Expand All @@ -10,5 +11,6 @@
- [Lints](lints.md)
- [Advanced features](advanced-features.md)
- [Unstable features](unstable-features.md)
- [Website features](website-features.md)
- [Passes](passes.md)
- [References](references.md)
13 changes: 10 additions & 3 deletions english/src/command-line-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ release: 1.17.0
LLVM version: 3.9
```

## `-o`/`--output`: output path
## `-o`/`--out-dir`: output directory path

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs -o target/doc
$ rustdoc src/lib.rs --output target/doc
$ rustdoc src/lib.rs --out-dir target/doc
```

By default, `rustdoc`'s output appears in a directory named `doc` in
Expand Down Expand Up @@ -94,7 +94,7 @@ $ rustdoc src/lib.rs --document-private-items
By default, `rustdoc` only documents items that are publicly reachable.

```rust
pub fn public() {} // this item is public and will documented
pub fn public() {} // this item is public and will be documented
mod private { // this item is private and will not be documented
pub fn unreachable() {} // this item is public, but unreachable, so it will not be documented
}
Expand Down Expand Up @@ -417,3 +417,10 @@ This flag is **deprecated** and **has no effect**.
Rustdoc only supports Rust source code and Markdown input formats. If the
file ends in `.md` or `.markdown`, `rustdoc` treats it as a Markdown file.
Otherwise, it assumes that the input file is Rust.

## `--nocapture`

When this flag is used with `--test`, the output (stdout and stderr) of your tests won't be
captured by rustdoc. Instead, the output will be directed to your terminal,
as if you had run the test executable manually. This is especially useful
for debugging your tests!
60 changes: 35 additions & 25 deletions english/src/documentation-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ conversion, so type inference fails because the type is not unique. Please note
that you must write the `(())` in one sequence without intermediate whitespace
so that `rustdoc` understands you want an implicit `Result`-returning function.
## Showing warnings in doctests
You can show warnings in doctests by running `rustdoc --test --test-args=--show-output`
(or, if you're using cargo, `cargo test --doc -- --show-output`).
By default, this will still hide `unused` warnings, since so many examples use private functions;
you can add `#![warn(unused)]` to the top of your example if you want to see unused variables or dead code warnings.
You can also use [`#![doc(test(attr(warn(unused))))]`][test-attr] in the crate root to enable warnings globally.
[test-attr]: ./the-doc-attribute.md#testattr
## Documenting macros
Here’s an example of documenting a macro:
Expand Down Expand Up @@ -297,20 +307,23 @@ we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
## Attributes
There are a few annotations that are useful to help `rustdoc` do the right
Code blocks can be annotated with attributes that help `rustdoc` do the right
thing when testing your code:
The `ignore` attribute tells Rust to ignore your code. This is almost never
what you want as it's the most generic. Instead, consider annotating it
with `text` if it's not code or using `#`s to get a working example that
only shows the part you care about.
```rust
/// ```ignore
/// fn foo() {
/// ```
# fn foo() {}
```
The `ignore` directive tells Rust to ignore your code. This is almost never
what you want, as it's the most generic. Instead, consider annotating it
with `text` if it's not code, or using `#`s to get a working example that
only shows the part you care about.
`should_panic` tells `rustdoc` that the code should compile correctly but
panic during execution. If the code doesn't panic, the test will fail.
```rust
/// ```should_panic
Expand All @@ -319,8 +332,11 @@ only shows the part you care about.
# fn foo() {}
```
`should_panic` tells `rustdoc` that the code should compile correctly, but
not actually pass as a test.
The `no_run` attribute will compile your code but not run it. This is
important for examples such as "Here's how to retrieve a web page,"
which you would want to ensure compiles, but might be run in a test
environment that has no network access. This attribute can also be
used to demonstrate code snippets that can cause Undefined Behavior.
```rust
/// ```no_run
Expand All @@ -331,24 +347,23 @@ not actually pass as a test.
# fn foo() {}
```
The `no_run` attribute will compile your code, but not run it. This is
important for examples such as "Here's how to retrieve a web page,"
which you would want to ensure compiles, but might be run in a test
environment that has no network access.
`compile_fail` tells `rustdoc` that the compilation should fail. If it
compiles, then the test will fail. However, please note that code failing
with the current Rust release may work in a future release, as new features
are added.
```text
```rust
/// ```compile_fail
/// let x = 5;
/// x += 2; // shouldn't compile!
/// ```
# fn foo() {}
```
`compile_fail` tells `rustdoc` that the compilation should fail. If it
compiles, then the test will fail. However please note that code failing
with the current Rust release may work in a future release, as new features
are added.
`edition2015`, `edition2018` and `edition2021` tell `rustdoc`
that the code sample should be compiled using the respective edition of Rust.
```text
```rust
/// Only runs on the 2018 edition.
///
/// ```edition2018
Expand All @@ -358,12 +373,9 @@ are added.
/// + "3".parse::<i32>()?
/// };
/// ```
# fn foo() {}
```
`edition2018` tells `rustdoc` that the code sample should be compiled using
the 2018 edition of Rust. Similarly, you can specify `edition2015` to compile
the code with the 2015 edition.
## Syntax reference
The *exact* syntax for code blocks, including the edge cases, can be found
Expand All @@ -385,7 +397,7 @@ section.

However, it's preferable to use fenced code blocks over indented code blocks.
Not only are fenced code blocks considered more idiomatic for Rust code,
but there is no way to use directives such as `ignore` or `should_panic` with
but there is no way to use attributes such as `ignore` or `should_panic` with
indented code blocks.

### Include items only when collecting doctests
Expand Down Expand Up @@ -424,9 +436,7 @@ without including it in your main documentation. For example, you could write th
`lib.rs` to test your README as part of your doctests:

```rust,no_run
#![feature(external_doc)]
#[doc(include = "../README.md")]
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
```
Expand Down
107 changes: 107 additions & 0 deletions english/src/how-to-read-rustdoc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# How to read rustdoc output

Rustdoc's HTML output includes a friendly and useful navigation interface which
makes it easier for users to navigate and understand your code.
This chapter covers the major features of that interface,
and is a great starting point for documentation authors and users alike.

## Structure

The `rustdoc` output is divided into three sections.
Along the left side of each page is a quick navigation bar,
which shows contextual information about the current entry.
The rest of the page is taken up by the search interface at the top
and the documentation for the current item below that.

## The Item Documentation

The majority of the screen is taken up with the documentation text for the item
currently being viewed.
At the top is some at-a-glance info and controls:

- the type and name of the item,
such as "Struct `std::time::Duration`",
- a button to copy the item's path to the clipboard,
which is a clipboard item
- a button to collapse or expand the top-level documentation for that item
(`[+]` or `[-]`),
- a link to the source code (`[src]`),
if [configured](the-doc-attribute.html#html_no_source),
and present (the source may not be available if
the documentation was created with `cargo doc --no-deps`),
- and the version in which the item became stable,
if it's a stable item in the standard library.

Below this is the main documentation for the item,
including a definition or function signature if appropriate,
followed by a list of fields or variants for Rust types.
Finally, the page lists associated functions and trait implementations,
including automatic and blanket implementations that `rustdoc` knows about.

### Navigation

Subheadings, variants, fields, and many other things in this documentation
are anchors and can be clicked on and deep-linked to,
which is a great way to communicate exactly what you're talking about.
The typograpical character "§" appears next to lines with anchors on them
when hovered or given keyboard focus.

## The Navigation Bar

For example, when looking at documentation for the crate root,
it shows all the crates documented in the documentation bundle,
and quick links to the modules, structs, traits, functions, and macros available
from the current crate.
At the top, it displays a [configurable logo](the-doc-attribute.html#html_logo_url)
alongside the current crate's name and version,
or the current item whose documentation is being displayed.

## The Theme Picker and Search Interface

When viewing `rustdoc`'s output in a browser with JavaScript enabled,
a dynamic interface appears at the top of the page.
To the left is the theme picker, denoted with a paint-brush icon,
and the search interface, help screen, and options appear to the right of that.

### The Theme Picker

Clicking on the theme picker provides a list of themes -
by default `ayu`, `light`, and `dark` -
which are available for viewing.

### The Search Interface

Typing in the search bar instantly searches the available documentation for
the string entered with a fuzzy matching algorithm that is tolerant of minor
typos.

By default, the search results give are "In Names",
meaning that the fuzzy match is made against the names of items.
Matching names are shown on the left, and the first few words of their
descriptions are given on the right.
By clicking an item, you will navigate to its particular documentation.

There are two other sets of results, shown as tabs in the search results pane.
"In Parameters" shows matches for the string in the types of parameters to
functions, and "In Return Types" shows matches in the return types of functions.
Both are very useful when looking for a function whose name you can't quite
bring to mind when you know the type you have or want.

When typing in the search bar, you can prefix your search term with a type
followed by a colon (such as `mod:`) to restrict the results to just that
kind of item. (The available items are listed in the help popup.)

### Shortcuts

Pressing `S` while focused elsewhere on the page will move focus to the
search bar, and pressing `?` shows the help screen,
which includes all these shortcuts and more.
Pressing `T` focuses the theme picker.

When the search results are focused,
the left and right arrows move between tabs and the up and down arrows move
among the results.
Pressing the enter or return key opens the highlighted result.

When looking at the documentation for an item, the plus and minus keys expand
and collapse all sections in the document.
10 changes: 4 additions & 6 deletions english/src/how-to-write-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,13 @@ Example:

```md
- [x] Complete task
- [ ] IncComplete task
- [ ] Incomplete task
```

This will render as
This will render as:

<ul>
<li><input type="checkbox"></li>
<li><input type="checkbox" checked></li>
</ul>
> - [x] Complete task
> - [ ] Incomplete task
See the specification for the [task list extension] for more details.

Expand Down
6 changes: 6 additions & 0 deletions english/src/lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ This lint **warns by default**. This lint detects when [intra-doc links] from pu
For example:

```rust
#![warn(rustdoc::private_intra_doc_links)] // note: unnecessary - warns by default.

/// [private]
pub fn public() {}
fn private() {}
Expand Down Expand Up @@ -227,6 +229,8 @@ This lint **warns by default**. It detects code block attributes in
documentation examples that have potentially mis-typed values. For example:
```rust
#![warn(rustdoc::invalid_codeblock_attributes)] // note: unnecessary - warns by default.
/// Example.
///
/// ```should-panic
Expand Down Expand Up @@ -344,6 +348,8 @@ This lint is **warn-by-default**. It detects URLs which are not links.
For example:
```rust
#![warn(rustdoc::bare_urls)] // note: unnecessary - warns by default.
/// http://example.org
/// [http://example.net]
pub fn foo() {}
Expand Down

0 comments on commit 466b14e

Please sign in to comment.