Improve rustdoc book
This commit is contained in:
parent
b97dc20784
commit
4e067e80eb
@ -4,3 +4,7 @@ title = "The rustdoc book"
|
||||
|
||||
[output.html]
|
||||
git-repository-url = "https://github.com/rust-lang/rust/tree/master/src/doc/rustdoc"
|
||||
|
||||
[output.html.redirect]
|
||||
"/the-doc-attribute.html" = "write-documentation/the-doc-attribute.html"
|
||||
"/documentation-tests.html" = "write-documentation/documentation-tests.html"
|
||||
|
@ -1,16 +1,15 @@
|
||||
# The Rustdoc Book
|
||||
|
||||
- [What is rustdoc?](what-is-rustdoc.md)
|
||||
- [Command-line arguments](command-line-arguments.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)
|
||||
- [The `#[doc]` attribute](the-doc-attribute.md)
|
||||
- [Documentation tests](documentation-tests.md)
|
||||
- [Linking to items by name](linking-to-items-by-name.md)
|
||||
- [Lints](lints.md)
|
||||
- [What to include (and exclude)](write-documentation/what-to-include.md)
|
||||
- [The `#[doc]` attribute](write-documentation/the-doc-attribute.md)
|
||||
- [Linking to items by name](write-documentation/linking-to-items-by-name.md)
|
||||
- [Documentation tests](write-documentation/documentation-tests.md)
|
||||
- [Rustdoc-specific lints](lints.md)
|
||||
- [Advanced features](advanced-features.md)
|
||||
- [Unstable features](unstable-features.md)
|
||||
- [Website features](website-features.md)
|
||||
- [Passes](passes.md)
|
||||
- [Deprecated features](deprecated-features.md)
|
||||
- [References](references.md)
|
||||
|
@ -88,3 +88,25 @@ You can add multiple aliases at the same time by using a list:
|
||||
#[doc(alias("x", "big"))]
|
||||
pub struct BigX;
|
||||
```
|
||||
|
||||
## Custom search engines
|
||||
|
||||
If you find yourself often referencing online Rust docs you might enjoy using a custom search
|
||||
engine. This allows you to use the navigation bar directly to search a `rustdoc` website.
|
||||
Most browsers support this feature by letting you define a URL template containing `%s`
|
||||
which will be substituted for the search term. As an example, for the standard library you could use
|
||||
this template:
|
||||
|
||||
```text
|
||||
https://doc.rust-lang.org/stable/std/?search=%s
|
||||
```
|
||||
|
||||
Note that this will take you to a results page listing all matches. If you want to navigate to the first
|
||||
result right away (which is often the best match) use the following instead:
|
||||
|
||||
```text
|
||||
https://doc.rust-lang.org/stable/std/?search=%s&go_to_first=true
|
||||
```
|
||||
|
||||
This URL adds the `go_to_first=true` query parameter which can be appended to any `rustdoc` search URL
|
||||
to automatically go to the first result.
|
||||
|
@ -177,7 +177,7 @@ $ rustdoc src/lib.rs --test
|
||||
```
|
||||
|
||||
This flag will run your code examples as tests. For more, see [the chapter
|
||||
on documentation tests](documentation-tests.md).
|
||||
on documentation tests](write-documentation/documentation-tests.md).
|
||||
|
||||
See also `--test-args`.
|
||||
|
||||
@ -190,7 +190,7 @@ $ rustdoc src/lib.rs --test --test-args ignored
|
||||
```
|
||||
|
||||
This flag will pass options to the test runner when running documentation tests.
|
||||
For more, see [the chapter on documentation tests](documentation-tests.md).
|
||||
For more, see [the chapter on documentation tests](write-documentation/documentation-tests.md).
|
||||
|
||||
See also `--test`.
|
||||
|
||||
@ -336,7 +336,7 @@ $ rustdoc src/lib.rs --sysroot /path/to/sysroot
|
||||
Similar to `rustc --sysroot`, this lets you change the sysroot `rustdoc` uses
|
||||
when compiling your code.
|
||||
|
||||
### `--edition`: control the edition of docs and doctests
|
||||
## `--edition`: control the edition of docs and doctests
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
@ -403,12 +403,12 @@ encoded as UTF-8.
|
||||
## `--passes`: add more rustdoc passes
|
||||
|
||||
This flag is **deprecated**.
|
||||
For more details on passes, see [the chapter on them](passes.md).
|
||||
For more details on passes, see [the chapter on them](deprecated-features.md#passes).
|
||||
|
||||
## `--no-defaults`: don't run default passes
|
||||
|
||||
This flag is **deprecated**.
|
||||
For more details on passes, see [the chapter on them](passes.md).
|
||||
For more details on passes, see [the chapter on them](deprecated-features.md#passes).
|
||||
|
||||
## `-r`/`--input-format`: input format
|
||||
|
||||
@ -417,23 +417,3 @@ 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.
|
||||
|
||||
# Unstable command line arguments
|
||||
|
||||
## `--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!
|
||||
|
||||
## `--check`
|
||||
|
||||
When this flag is supplied, rustdoc will type check and lint your code, but will not generate any
|
||||
documentation or run your doctests.
|
||||
|
||||
Using this flag looks like:
|
||||
|
||||
```bash
|
||||
rustdoc -Z unstable-options --check src/lib.rs
|
||||
```
|
||||
|
@ -1,4 +1,6 @@
|
||||
# Passes
|
||||
# Deprecated features
|
||||
|
||||
## Passes
|
||||
|
||||
Rustdoc has a concept called "passes". These are transformations that
|
||||
`rustdoc` runs on your documentation before producing its final output.
|
@ -26,7 +26,7 @@ At the top is some at-a-glance info and controls:
|
||||
- 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),
|
||||
if [configured](write-documentation/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,
|
||||
@ -52,7 +52,7 @@ 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)
|
||||
At the top, it displays a [configurable logo](write-documentation/the-doc-attribute.html#html_logo_url)
|
||||
alongside the current crate's name and version,
|
||||
or the current item whose documentation is being displayed.
|
||||
|
||||
|
@ -13,11 +13,11 @@ Note that, except for `missing_docs`, these lints are only available when runnin
|
||||
|
||||
Here is the list of the lints provided by `rustdoc`:
|
||||
|
||||
## broken_intra_doc_links
|
||||
## `broken_intra_doc_links`
|
||||
|
||||
This lint **warns by default**. This lint detects when an [intra-doc link] fails to be resolved. For example:
|
||||
|
||||
[intra-doc link]: linking-to-items-by-name.md
|
||||
[intra-doc link]: write-documentation/linking-to-items-by-name.md
|
||||
|
||||
```rust
|
||||
/// I want to link to [`Nonexistent`] but it doesn't exist!
|
||||
@ -64,7 +64,7 @@ help: to link to the function, add parentheses
|
||||
|
||||
```
|
||||
|
||||
## private_intra_doc_links
|
||||
## `private_intra_doc_links`
|
||||
|
||||
This lint **warns by default**. This lint detects when [intra-doc links] from public to private items.
|
||||
For example:
|
||||
@ -104,9 +104,9 @@ warning: public documentation for `public` links to private item `private`
|
||||
= note: this link resolves only because you passed `--document-private-items`, but will break without
|
||||
```
|
||||
|
||||
[intra-doc links]: linking-to-items-by-name.html
|
||||
[intra-doc links]: write-documentation/linking-to-items-by-name.md
|
||||
|
||||
## missing_docs
|
||||
## `missing_docs`
|
||||
|
||||
This lint is **allowed by default**. It detects items missing documentation.
|
||||
For example:
|
||||
@ -130,7 +130,7 @@ warning: missing documentation for a function
|
||||
|
||||
Note that unlike other rustdoc lints, this lint is also available from `rustc` directly.
|
||||
|
||||
## missing_crate_level_docs
|
||||
## `missing_crate_level_docs`
|
||||
|
||||
This lint is **allowed by default**. It detects if there is no documentation
|
||||
at the crate root. For example:
|
||||
@ -154,7 +154,7 @@ warning in the future. This is intended as a means to introduce new users on
|
||||
get started, without providing overwhelming warnings like `missing_docs`
|
||||
might.
|
||||
|
||||
## missing_doc_code_examples
|
||||
## `missing_doc_code_examples`
|
||||
|
||||
This lint is **allowed by default** and is **nightly-only**. It detects when a documentation block
|
||||
is missing a code example. For example:
|
||||
@ -190,7 +190,7 @@ To fix the lint, you need to add a code example into the documentation block:
|
||||
pub fn no_code_example() {}
|
||||
```
|
||||
|
||||
## private_doc_tests
|
||||
## `private_doc_tests`
|
||||
|
||||
This lint is **allowed by default**. It detects documentation tests when they
|
||||
are on a private item. For example:
|
||||
@ -223,7 +223,7 @@ warning: Documentation test in private item
|
||||
| |___________^
|
||||
```
|
||||
|
||||
## invalid_codeblock_attributes
|
||||
## `invalid_codeblock_attributes`
|
||||
|
||||
This lint **warns by default**. It detects code block attributes in
|
||||
documentation examples that have potentially mis-typed values. For example:
|
||||
@ -259,7 +259,7 @@ warning: unknown attribute `should-panic`. Did you mean `should_panic`?
|
||||
In the example above, the correct form is `should_panic`. This helps detect
|
||||
typo mistakes for some common attributes.
|
||||
|
||||
## invalid_html_tags
|
||||
## `invalid_html_tags`
|
||||
|
||||
This lint is **allowed by default** and is **nightly-only**. It detects unclosed
|
||||
or invalid HTML tags. For example:
|
||||
@ -298,7 +298,7 @@ warning: unclosed HTML tag `h1`
|
||||
warning: 2 warnings emitted
|
||||
```
|
||||
|
||||
## invalid_rust_codeblocks
|
||||
## `invalid_rust_codeblocks`
|
||||
|
||||
This lint **warns by default**. It detects Rust code blocks in documentation
|
||||
examples that are invalid (e.g. empty, not parsable as Rust). For example:
|
||||
@ -342,7 +342,7 @@ warning: could not parse code block as Rust code
|
||||
= note: error from rustc: unterminated character literal
|
||||
```
|
||||
|
||||
## bare_urls
|
||||
## `bare_urls`
|
||||
|
||||
This lint is **warn-by-default**. It detects URLs which are not links.
|
||||
For example:
|
||||
|
@ -22,7 +22,7 @@ As detailed in [the chapter on documentation tests][doctest-attributes], you can
|
||||
nightly, you can optionally add an error number to state that a doctest should emit a specific error
|
||||
number:
|
||||
|
||||
[doctest-attributes]: documentation-tests.html#attributes
|
||||
[doctest-attributes]: write-documentation/documentation-tests.html#attributes
|
||||
|
||||
``````markdown
|
||||
```compile_fail,E0044
|
||||
@ -45,6 +45,8 @@ and enabled with a `#![feature(...)]` attribute in your crate.
|
||||
|
||||
### `#[doc(cfg)]`: Recording what platforms or features are required for code to be present
|
||||
|
||||
* Tracking issue: [#43781](https://github.com/rust-lang/rust/issues/43781)
|
||||
|
||||
You can use `#[doc(cfg(...))]` to tell Rustdoc exactly which platform items appear on.
|
||||
This has two effects:
|
||||
|
||||
@ -86,6 +88,8 @@ Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
|
||||
|
||||
### `doc_auto_cfg`: Automatically generate `#[doc(cfg)]`
|
||||
|
||||
* Tracking issue: [#43781](https://github.com/rust-lang/rust/issues/43781)
|
||||
|
||||
`doc_auto_cfg` is an extension to the `#[doc(cfg)]` feature. With it, you don't need to add
|
||||
`#[doc(cfg(...)]` anymore unless you want to override the default behaviour. So if we take the
|
||||
previous source code:
|
||||
@ -123,6 +127,8 @@ And `doc` won't show up anymore!
|
||||
|
||||
### Adding your trait to the "Notable traits" dialog
|
||||
|
||||
* Tracking issue: [#45040](https://github.com/rust-lang/rust/issues/45040)
|
||||
|
||||
Rustdoc keeps a list of a few traits that are believed to be "fundamental" to
|
||||
types that implement them. These traits are intended to be the primary interface
|
||||
for their implementers, and are often most of the API available to be documented
|
||||
@ -146,6 +152,8 @@ and [its tracking issue][issue-notable_trait].
|
||||
|
||||
### Exclude certain dependencies from documentation
|
||||
|
||||
* Tracking issue: [#44027](https://github.com/rust-lang/rust/issues/44027)
|
||||
|
||||
The standard library uses several dependencies which, in turn, use several types and traits from the
|
||||
standard library. In addition, there are several compiler-internal crates that are not considered to
|
||||
be part of the official standard library, and thus would be a distraction to include in
|
||||
@ -164,8 +172,7 @@ Book][unstable-masked] and [its tracking issue][issue-masked].
|
||||
[unstable-masked]: ../unstable-book/language-features/doc-masked.html
|
||||
[issue-masked]: https://github.com/rust-lang/rust/issues/44027
|
||||
|
||||
|
||||
## Document primitives
|
||||
### Document primitives
|
||||
|
||||
This is for Rust compiler internal use only.
|
||||
|
||||
@ -174,7 +181,7 @@ attributes. The `#[doc(primitive)]` attribute is used by the standard library to
|
||||
to generate documentation for primitive types, and requires `#![feature(rustdoc_internals)]` to
|
||||
enable.
|
||||
|
||||
## Document keywords
|
||||
### Document keywords
|
||||
|
||||
This is for Rust compiler internal use only.
|
||||
|
||||
@ -199,6 +206,8 @@ the flag in question to Rustdoc on the command-line. To do this from Cargo, you
|
||||
|
||||
### `--markdown-before-content`: include rendered Markdown before the content
|
||||
|
||||
* Tracking issue: [#44027](https://github.com/rust-lang/rust/issues/44027)
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
```bash
|
||||
@ -241,7 +250,7 @@ attribute][doc-playground]. Please be aware that the official Rust Playground at
|
||||
https://play.rust-lang.org does not have every crate available, so if your examples require your
|
||||
crate, make sure the playground you provide has your crate available.
|
||||
|
||||
[doc-playground]: the-doc-attribute.html#html_playground_url
|
||||
[doc-playground]: write-documentation/the-doc-attribute.html#html_playground_url
|
||||
|
||||
If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone
|
||||
Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both
|
||||
@ -279,6 +288,8 @@ between compilations.
|
||||
|
||||
### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs
|
||||
|
||||
* Tracking issue: [#54765](https://github.com/rust-lang/rust/issues/54765)
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
```bash
|
||||
@ -331,6 +342,24 @@ Using `index-page` option enables `enable-index-page` option as well.
|
||||
|
||||
This feature allows the generation of a default index-page which lists the generated crates.
|
||||
|
||||
### `--nocapture`: disable output capture for test
|
||||
|
||||
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!
|
||||
|
||||
### `--check`: only checks the documentation
|
||||
|
||||
When this flag is supplied, rustdoc will type check and lint your code, but will not generate any
|
||||
documentation or run your doctests.
|
||||
|
||||
Using this flag looks like:
|
||||
|
||||
```bash
|
||||
rustdoc -Z unstable-options --check src/lib.rs
|
||||
```
|
||||
|
||||
### `--static-root-path`: control how static files are loaded in HTML output
|
||||
|
||||
Using this flag looks like this:
|
||||
@ -348,6 +377,8 @@ renamed with `--resource-suffix` will load from the given path.
|
||||
|
||||
### `--persist-doctests`: persist doctest executables after running
|
||||
|
||||
* Tracking issue: [#56925](https://github.com/rust-lang/rust/issues/56925)
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
```bash
|
||||
@ -360,6 +391,8 @@ with this option, you can keep those binaries around for farther testing.
|
||||
|
||||
### `--show-coverage`: calculate the percentage of items with documentation
|
||||
|
||||
* Tracking issue: [#58154](https://github.com/rust-lang/rust/issues/58154)
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
```bash
|
||||
@ -438,6 +471,8 @@ information.
|
||||
|
||||
### `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests
|
||||
|
||||
* Tracking issue: [#64245](https://github.com/rust-lang/rust/issues/64245)
|
||||
|
||||
Using this flag looks like this:
|
||||
|
||||
```bash
|
||||
@ -471,6 +506,8 @@ override `ignore`.
|
||||
|
||||
### `--runtool`, `--runtool-arg`: program to run tests with; args to pass to it
|
||||
|
||||
* Tracking issue: [#64245](https://github.com/rust-lang/rust/issues/64245)
|
||||
|
||||
Using these options looks like this:
|
||||
|
||||
```bash
|
||||
@ -488,6 +525,8 @@ Another use case would be to run a test inside an emulator, or through a Virtual
|
||||
|
||||
### `--with-examples`: include examples of uses of items as documentation
|
||||
|
||||
* Tracking issue: [#88791](https://github.com/rust-lang/rust/issues/88791)
|
||||
|
||||
This option, combined with `--scrape-examples-target-crate` and
|
||||
`--scrape-examples-output-path`, is used to implement the functionality in [RFC
|
||||
#3123](https://github.com/rust-lang/rfcs/pull/3123). Uses of an item (currently
|
||||
@ -515,6 +554,8 @@ add the `--scrape-tests` flag.
|
||||
|
||||
### `--check-cfg`: check configuration flags
|
||||
|
||||
* Tracking issue: [#82450](https://github.com/rust-lang/rust/issues/82450)
|
||||
|
||||
This flag accepts the same values as `rustc --check-cfg`, and uses it to check configuration flags.
|
||||
|
||||
Using this flag looks like this:
|
||||
|
@ -1,25 +0,0 @@
|
||||
# Website features
|
||||
|
||||
These features are about using the website generated by `rustdoc`.
|
||||
|
||||
## Custom search engines
|
||||
|
||||
If you find yourself often referencing online Rust docs you might enjoy using a custom search
|
||||
engine. This allows you to use the navigation bar directly to search a `rustdoc` website.
|
||||
Most browsers support this feature by letting you define a URL template containing `%s`
|
||||
which will be substituted for the search term. As an example, for the standard library you could use
|
||||
this template:
|
||||
|
||||
```text
|
||||
https://doc.rust-lang.org/stable/std/?search=%s
|
||||
```
|
||||
|
||||
Note that this will take you to a results page listing all matches. If you want to navigate to the first
|
||||
result right away (which is often the best match) use the following instead:
|
||||
|
||||
```text
|
||||
https://doc.rust-lang.org/stable/std/?search=%s&go_to_first=true
|
||||
```
|
||||
|
||||
This URL adds the `go_to_first=true` query parameter which can be appended to any `rustdoc` search URL
|
||||
to automatically go to the first result.
|
@ -269,7 +269,7 @@ By default, this will still hide `unused` warnings, since so many examples use p
|
||||
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
|
||||
[test-attr]: the-doc-attribute.md#testattr
|
||||
|
||||
## Documenting macros
|
||||
|
@ -35,7 +35,7 @@ link to `Option`.
|
||||
You can refer to anything in scope, and use paths, including `Self`, `self`, `super`, and
|
||||
`crate`. Associated items (functions, types, and constants) are supported, but [not for blanket
|
||||
trait implementations][#79682]. Rustdoc also supports linking to all primitives listed in
|
||||
[the standard library documentation](../std/index.html#primitives).
|
||||
[the standard library documentation](../../std/index.html#primitives).
|
||||
|
||||
[#79682]: https://github.com/rust-lang/rust/pull/79682
|
||||
|
@ -38,7 +38,7 @@ but given that docs are rendered via Markdown, it will remove these newlines.
|
||||
Another use case is for including external files as documentation:
|
||||
|
||||
```rust,no_run
|
||||
#[doc = include_str!("../README.md")]
|
||||
#[doc = include_str!("../../README.md")]
|
||||
# fn f() {}
|
||||
```
|
||||
|
||||
@ -73,7 +73,7 @@ left hand side of the docs.
|
||||
#![doc(html_logo_url = "https://example.com/logo.jpg")]
|
||||
```
|
||||
|
||||
This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into
|
||||
This will put `<a href='../index.html'><img src='{}' alt='logo' width='100'></a>` into
|
||||
your docs, where the string for the attribute goes into the `{}`.
|
||||
|
||||
If you don't use this attribute, there will be no logo.
|
@ -122,4 +122,4 @@ Here is an example of a new theme, [Ayu].
|
||||
[API Guidelines]: https://rust-lang.github.io/api-guidelines/documentation.html#rustdoc-does-not-show-unhelpful-implementation-details-c-hidden
|
||||
[Documentation tests]: documentation-tests.md
|
||||
[on this blog]: https://blog.guillaume-gomez.fr/articles/2016-09-16+Generating+doc+with+rustdoc+and+a+custom+theme
|
||||
[rustdoc-lints]: lints.md
|
||||
[rustdoc-lints]: ../lints.md
|
Loading…
x
Reference in New Issue
Block a user