]> git.lizzy.rs Git - rust.git/blob - src/doc/rustdoc/src/unstable-features.md
Add documentation for --show-coverage option
[rust.git] / src / doc / rustdoc / src / unstable-features.md
1 # Unstable features
2
3 Rustdoc is under active development, and like the Rust compiler, some features are only available
4 on nightly releases. Some of these features are new and need some more testing before they're able to be
5 released to the world at large, and some of them are tied to features in the Rust compiler that are unstable. Several features here require a matching `#![feature(...)]` attribute to
6 enable, and thus are more fully documented in the [Unstable Book]. Those sections will link over
7 there as necessary.
8
9 [Unstable Book]: ../unstable-book/index.html
10
11 ## Nightly-gated functionality
12
13 These features just require a nightly build to operate. Unlike the other features on this page,
14 these don't need to be "turned on" with a command-line flag or a `#![feature(...)]` attribute in
15 your crate. This can give them some subtle fallback modes when used on a stable release, so be
16 careful!
17
18 ### Error numbers for `compile-fail` doctests
19
20 As detailed in [the chapter on documentation tests][doctest-attributes], you can add a
21 `compile_fail` attribute to a doctest to state that the test should fail to compile. However, on
22 nightly, you can optionally add an error number to state that a doctest should emit a specific error
23 number:
24
25 [doctest-attributes]: documentation-tests.html#attributes
26
27 ``````markdown
28 ```compile_fail,E0044
29 extern { fn some_func<T>(x: T); }
30 ```
31 ``````
32
33 This is used by the error index to ensure that the samples that correspond to a given error number
34 properly emit that error code. However, these error codes aren't guaranteed to be the only thing
35 that a piece of code emits from version to version, so this is unlikely to be stabilized in the
36 future.
37
38 Attempting to use these error numbers on stable will result in the code sample being interpreted as
39 plain text.
40
41 ### Linking to items by name
42
43 Rustdoc is capable of directly linking to other rustdoc pages in Markdown documentation using the path of item as a link.
44
45 For example, in the following code all of the links will link to the rustdoc page for `Bar`:
46
47 ```rust
48 /// This struct is not [Bar]
49 pub struct Foo1;
50
51 /// This struct is also not [bar](Bar)
52 pub struct Foo2;
53
54 /// This struct is also not [bar][b]
55 ///
56 /// [b]: Bar
57 pub struct Foo3;
58
59 /// This struct is also not [`Bar`]
60 pub struct Foo4;
61
62 pub struct Bar;
63 ```
64
65 You can refer to anything in scope, and use paths, including `Self`. You may also use `foo()` and `foo!()` to refer to methods/functions and macros respectively.
66
67 ```rust,edition2018
68 use std::sync::mpsc::Receiver;
69
70 /// This is an version of [`Receiver`], with support for [`std::future`].
71 ///
72 /// You can obtain a [`std::future::Future`] by calling [`Self::recv()`].
73 pub struct AsyncReceiver<T> {
74     sender: Receiver<T>
75 }
76
77 impl<T> AsyncReceiver<T> {
78     pub async fn recv() -> T {
79         unimplemented!()
80     }
81 }
82 ```
83
84 Paths in Rust have three namespaces: type, value, and macro. Items from these namespaces are allowed to overlap. In case of ambiguity, rustdoc will warn about the ambiguity and ask you to disambiguate, which can be done by using a prefix like `struct@`, `enum@`, `type@`, `trait@`, `union@`, `const@`, `static@`, `value@`, `function@`, `mod@`, `fn@`, `module@`, `method@` , `macro@`, or `derive@`:
85
86 ```rust
87 /// See also: [`Foo`](struct@Foo)
88 struct Bar;
89
90 /// This is different from [`Foo`](fn@Foo)
91 struct Foo {}
92
93 fn Foo() {}
94 ```
95
96 Note: Because of how `macro_rules` macros are scoped in Rust, the intra-doc links of a `macro_rules` macro will be resolved relative to the crate root, as opposed to the module it is defined in.
97
98 ## Extensions to the `#[doc]` attribute
99
100 These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
101 and enabled with a `#![feature(...)]` attribute in your crate.
102
103 ### Documenting platform-/feature-specific information
104
105 Because of the way Rustdoc documents a crate, the documentation it creates is specific to the target
106 rustc compiles for. Anything that's specific to any other target is dropped via `#[cfg]` attribute
107 processing early in the compilation process. However, Rustdoc has a trick up its sleeve to handle
108 platform-specific code if it *does* receive it.
109
110 Because Rustdoc doesn't need to fully compile a crate to binary, it replaces function bodies with
111 `loop {}` to prevent having to process more than necessary. This means that any code within a
112 function that requires platform-specific pieces is ignored. Combined with a special attribute,
113 `#[doc(cfg(...))]`, you can tell Rustdoc exactly which platform something is supposed to run on,
114 ensuring that doctests are only run on the appropriate platforms.
115
116 The `#[doc(cfg(...))]` attribute has another effect: When Rustdoc renders documentation for that
117 item, it will be accompanied by a banner explaining that the item is only available on certain
118 platforms.
119
120 For Rustdoc to document an item, it needs to see it, regardless of what platform it's currently
121 running on. To aid this, Rustdoc sets the flag `#[cfg(doc)]` when running on your crate.
122 Combining this with the target platform of a given item allows it to appear when building your crate
123 normally on that platform, as well as when building documentation anywhere.
124
125 For example, `#[cfg(any(windows, doc))]` will preserve the item either on Windows or during the
126 documentation process. Then, adding a new attribute `#[doc(cfg(windows))]` will tell Rustdoc that
127 the item is supposed to be used on Windows. For example:
128
129 ```rust
130 #![feature(doc_cfg)]
131
132 /// Token struct that can only be used on Windows.
133 #[cfg(any(windows, doc))]
134 #[doc(cfg(windows))]
135 pub struct WindowsToken;
136
137 /// Token struct that can only be used on Unix.
138 #[cfg(any(unix, doc))]
139 #[doc(cfg(unix))]
140 pub struct UnixToken;
141 ```
142
143 In this sample, the tokens will only appear on their respective platforms, but they will both appear
144 in documentation.
145
146 `#[doc(cfg(...))]` was introduced to be used by the standard library and currently requires the
147 `#![feature(doc_cfg)]` feature gate. For more information, see [its chapter in the Unstable
148 Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
149
150 [unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
151 [issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
152
153 ### Adding your trait to the "Important Traits" dialog
154
155 Rustdoc keeps a list of a few traits that are believed to be "fundamental" to a given type when
156 implemented on it. These traits are intended to be the primary interface for their types, and are
157 often the only thing available to be documented on their types. For this reason, Rustdoc will track
158 when a given type implements one of these traits and call special attention to it when a function
159 returns one of these types. This is the "Important Traits" dialog, visible as a circle-i button next
160 to the function, which, when clicked, shows the dialog.
161
162 In the standard library, the traits that qualify for inclusion are `Iterator`, `io::Read`, and
163 `io::Write`. However, rather than being implemented as a hard-coded list, these traits have a
164 special marker attribute on them: `#[doc(spotlight)]`. This means that you could apply this
165 attribute to your own trait to include it in the "Important Traits" dialog in documentation.
166
167 The `#[doc(spotlight)]` attribute currently requires the `#![feature(doc_spotlight)]` feature gate.
168 For more information, see [its chapter in the Unstable Book][unstable-spotlight] and [its tracking
169 issue][issue-spotlight].
170
171 [unstable-spotlight]: ../unstable-book/language-features/doc-spotlight.html
172 [issue-spotlight]: https://github.com/rust-lang/rust/issues/45040
173
174 ### Exclude certain dependencies from documentation
175
176 The standard library uses several dependencies which, in turn, use several types and traits from the
177 standard library. In addition, there are several compiler-internal crates that are not considered to
178 be part of the official standard library, and thus would be a distraction to include in
179 documentation. It's not enough to exclude their crate documentation, since information about trait
180 implementations appears on the pages for both the type and the trait, which can be in different
181 crates!
182
183 To prevent internal types from being included in documentation, the standard library adds an
184 attribute to their `extern crate` declarations: `#[doc(masked)]`. This causes Rustdoc to "mask out"
185 types from these crates when building lists of trait implementations.
186
187 The `#[doc(masked)]` attribute is intended to be used internally, and requires the
188 `#![feature(doc_masked)]` feature gate.  For more information, see [its chapter in the Unstable
189 Book][unstable-masked] and [its tracking issue][issue-masked].
190
191 [unstable-masked]: ../unstable-book/language-features/doc-masked.html
192 [issue-masked]: https://github.com/rust-lang/rust/issues/44027
193
194 ### Include external files as API documentation
195
196 As designed in [RFC 1990], Rustdoc can read an external file to use as a type's documentation. This
197 is useful if certain documentation is so long that it would break the flow of reading the source.
198 Instead of writing it all inline, writing `#[doc(include = "sometype.md")]` will ask Rustdoc to
199 instead read that file and use it as if it were written inline.
200
201 [RFC 1990]: https://github.com/rust-lang/rfcs/pull/1990
202
203 `#[doc(include = "...")]` currently requires the `#![feature(external_doc)]` feature gate. For more
204 information, see [its chapter in the Unstable Book][unstable-include] and [its tracking
205 issue][issue-include].
206
207 [unstable-include]: ../unstable-book/language-features/external-doc.html
208 [issue-include]: https://github.com/rust-lang/rust/issues/44732
209
210 ### Add aliases for an item in documentation search
211
212 This feature allows you to add alias(es) to an item when using the `rustdoc` search through the
213 `doc(alias)` attribute. Example:
214
215 ```rust,no_run
216 #![feature(doc_alias)]
217
218 #[doc(alias = "x")]
219 #[doc(alias = "big")]
220 pub struct BigX;
221 ```
222
223 Then, when looking for it through the `rustdoc` search, if you enter "x" or
224 "big", search will show the `BigX` struct first.
225
226 ## Unstable command-line arguments
227
228 These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are
229 themselves marked as unstable. To use any of these options, pass `-Z unstable-options` as well as
230 the flag in question to Rustdoc on the command-line. To do this from Cargo, you can either use the
231 `RUSTDOCFLAGS` environment variable or the `cargo rustdoc` command.
232
233 ### `--markdown-before-content`: include rendered Markdown before the content
234
235 Using this flag looks like this:
236
237 ```bash
238 $ rustdoc src/lib.rs -Z unstable-options --markdown-before-content extra.md
239 $ rustdoc README.md -Z unstable-options --markdown-before-content extra.md
240 ```
241
242 Just like `--html-before-content`, this allows you to insert extra content inside the `<body>` tag
243 but before the other content `rustdoc` would normally produce in the rendered documentation.
244 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
245 Markdown renderer before inserting the result into the file.
246
247 ### `--markdown-after-content`: include rendered Markdown after the content
248
249 Using this flag looks like this:
250
251 ```bash
252 $ rustdoc src/lib.rs -Z unstable-options --markdown-after-content extra.md
253 $ rustdoc README.md -Z unstable-options --markdown-after-content extra.md
254 ```
255
256 Just like `--html-after-content`, this allows you to insert extra content before the `</body>` tag
257 but after the other content `rustdoc` would normally produce in the rendered documentation.
258 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
259 Markdown renderer before inserting the result into the file.
260
261 ### `--playground-url`: control the location of the playground
262
263 Using this flag looks like this:
264
265 ```bash
266 $ rustdoc src/lib.rs -Z unstable-options --playground-url https://play.rust-lang.org/
267 ```
268
269 When rendering a crate's docs, this flag gives the base URL of the Rust Playground, to use for
270 generating `Run` buttons. Unlike `--markdown-playground-url`, this argument works for standalone
271 Markdown files *and* Rust crates. This works the same way as adding `#![doc(html_playground_url =
272 "url")]` to your crate root, as mentioned in [the chapter about the `#[doc]`
273 attribute][doc-playground]. Please be aware that the official Rust Playground at
274 https://play.rust-lang.org does not have every crate available, so if your examples require your
275 crate, make sure the playground you provide has your crate available.
276
277 [doc-playground]: the-doc-attribute.html#html_playground_url
278
279 If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone
280 Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both
281 `--playground-url` and `#![doc(html_playground_url = "url")]` are present when rendering crate docs,
282 the attribute will take precedence.
283
284 ### `--sort-modules-by-appearance`: control how items on module pages are sorted
285
286 Using this flag looks like this:
287
288 ```bash
289 $ rustdoc src/lib.rs -Z unstable-options --sort-modules-by-appearance
290 ```
291
292 Ordinarily, when `rustdoc` prints items in module pages, it will sort them alphabetically (taking
293 some consideration for their stability, and names that end in a number). Giving this flag to
294 `rustdoc` will disable this sorting and instead make it print the items in the order they appear in
295 the source.
296
297 ### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs
298
299 Using this flag looks like this:
300
301 ```bash
302 $ rustdoc src/lib.rs -Z unstable-options --resource-suffix suf
303 ```
304
305 When rendering docs, `rustdoc` creates several CSS and JavaScript files as part of the output. Since
306 all these files are linked from every page, changing where they are can be cumbersome if you need to
307 specially cache them. This flag will rename all these files in the output to include the suffix in
308 the filename. For example, `light.css` would become `light-suf.css` with the above command.
309
310 ### `--display-warnings`: display warnings when documenting or running documentation tests
311
312 Using this flag looks like this:
313
314 ```bash
315 $ rustdoc src/lib.rs -Z unstable-options --display-warnings
316 $ rustdoc --test src/lib.rs -Z unstable-options --display-warnings
317 ```
318
319 The intent behind this flag is to allow the user to see warnings that occur within their library or
320 their documentation tests, which are usually suppressed. However, [due to a
321 bug][issue-display-warnings], this flag doesn't 100% work as intended. See the linked issue for
322 details.
323
324 [issue-display-warnings]: https://github.com/rust-lang/rust/issues/41574
325
326 ### `--extern-html-root-url`: control how rustdoc links to non-local crates
327
328 Using this flag looks like this:
329
330 ```bash
331 $ rustdoc src/lib.rs -Z unstable-options --extern-html-root-url some-crate=https://example.com/some-crate/1.0.1
332 ```
333
334 Ordinarily, when rustdoc wants to link to a type from a different crate, it looks in two places:
335 docs that already exist in the output directory, or the `#![doc(doc_html_root)]` set in the other
336 crate. However, if you want to link to docs that exist in neither of those places, you can use these
337 flags to control that behavior. When the `--extern-html-root-url` flag is given with a name matching
338 one of your dependencies, rustdoc use that URL for those docs. Keep in mind that if those docs exist
339 in the output directory, those local docs will still override this flag.
340
341 ### `-Z force-unstable-if-unmarked`
342
343 Using this flag looks like this:
344
345 ```bash
346 $ rustdoc src/lib.rs -Z force-unstable-if-unmarked
347 ```
348
349 This is an internal flag intended for the standard library and compiler that applies an
350 `#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This
351 allows `rustdoc` to be able to generate documentation for the compiler crates and the standard
352 library, as an equivalent command-line argument is provided to `rustc` when building those crates.
353
354 ### `--index-page`: provide a top-level landing page for docs
355
356 This feature allows you to generate an index-page with a given markdown file. A good example of it
357 is the [rust documentation index](https://doc.rust-lang.org/nightly/index.html).
358
359 With this, you'll have a page which you can custom as much as you want at the top of your crates.
360
361 Using `index-page` option enables `enable-index-page` option as well.
362
363 ### `--enable-index-page`: generate a default index page for docs
364
365 This feature allows the generation of a default index-page which lists the generated crates.
366
367 ### `--static-root-path`: control how static files are loaded in HTML output
368
369 Using this flag looks like this:
370
371 ```bash
372 $ rustdoc src/lib.rs -Z unstable-options --static-root-path '/cache/'
373 ```
374
375 This flag controls how rustdoc links to its static files on HTML pages. If you're hosting a lot of
376 crates' docs generated by the same version of rustdoc, you can use this flag to cache rustdoc's CSS,
377 JavaScript, and font files in a single location, rather than duplicating it once per "doc root"
378 (grouping of crate docs generated into the same output directory, like with `cargo doc`). Per-crate
379 files like the search index will still load from the documentation root, but anything that gets
380 renamed with `--resource-suffix` will load from the given path.
381
382 ### `--persist-doctests`: persist doctest executables after running
383
384 Using this flag looks like this:
385
386 ```bash
387 $ rustdoc src/lib.rs --test -Z unstable-options --persist-doctests target/rustdoctest
388 ```
389
390 This flag allows you to keep doctest executables around after they're compiled or run.
391 Usually, rustdoc will immediately discard a compiled doctest after it's been tested, but
392 with this option, you can keep those binaries around for farther testing.
393
394 ### `--show-coverage`: calculate the percentage of items with documentation
395
396 Using this flag looks like this:
397
398 ```bash
399 $ rustdoc src/lib.rs -Z unstable-options --show-coverage
400 ```
401
402 If you want to determine how many items in your crate are documented, pass this flag to rustdoc.
403 When it receives this flag, it will count the public items in your crate that have documentation,
404 and print out the counts and a percentage instead of generating docs.
405
406 Some methodology notes about what rustdoc counts in this metric:
407
408 * Rustdoc will only count items from your crate (i.e. items re-exported from other crates don't
409   count).
410 * Docs written directly onto inherent impl blocks are not counted, even though their doc comments
411   are displayed, because the common pattern in Rust code is to write all inherent methods into the
412   same impl block.
413 * Items in a trait implementation are not counted, as those impls will inherit any docs from the
414   trait itself.
415 * By default, only public items are counted. To count private items as well, pass
416   `--document-private-items` at the same time.
417
418 Public items that are not documented can be seen with the built-in `missing_docs` lint. Private
419 items that are not documented can be seen with Clippy's `missing_docs_in_private_items` lint.
420
421 ### `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests
422
423 Using this flag looks like this:
424
425 ```bash
426 $ rustdoc src/lib.rs -Z unstable-options --enable-per-target-ignores
427 ```
428
429 This flag allows you to tag doctests with compiltest style `ignore-foo` filters that prevent
430 rustdoc from running that test if the target triple string contains foo. For example:
431
432 ```rust
433 ///```ignore-foo,ignore-bar
434 ///assert!(2 == 2);
435 ///```
436 struct Foo;
437 ```
438
439 This will not be run when the build target is `super-awesome-foo` or `less-bar-awesome`.
440 If the flag is not enabled, then rustdoc will consume the filter, but do nothing with it, and
441 the above example will be run for all targets.
442 If you want to preserve backwards compatibility for older versions of rustdoc, you can use
443
444 ```rust
445 ///```ignore,ignore-foo
446 ///assert!(2 == 2);
447 ///```
448 struct Foo;
449 ```
450
451 In older versions, this will be ignored on all targets, but on newer versions `ignore-gnu` will
452 override `ignore`.
453
454 ### `--runtool`, `--runtool-arg`: program to run tests with; args to pass to it
455
456 Using these options looks like this:
457
458 ```bash
459 $ rustdoc src/lib.rs -Z unstable-options --runtool runner --runtool-arg --do-thing --runtool-arg --do-other-thing
460 ```
461
462 These options can be used to run the doctest under a program, and also pass arguments to
463 that program. For example, if you want to run your doctests under valgrind you might run
464
465 ```bash
466 $ rustdoc src/lib.rs -Z unstable-options --runtool valgrind
467 ```
468
469 Another use case would be to run a test inside an emulator, or through a Virtual Machine.
470
471 ### `--show-coverage`: get statistics about code documentation coverage
472
473 This option allows you to get a nice overview over your code documentation coverage, including both
474 doc-comments and code examples in the doc-comments. Example:
475
476 ```bash
477 $ rustdoc src/lib.rs -Z unstable-options --show-coverage
478 +-------------------------------------+------------+------------+------------+------------+
479 | File                                | Documented | Percentage |   Examples | Percentage |
480 +-------------------------------------+------------+------------+------------+------------+
481 | lib.rs                              |          4 |     100.0% |          1 |      25.0% |
482 +-------------------------------------+------------+------------+------------+------------+
483 | Total                               |          4 |     100.0% |          1 |      25.0% |
484 +-------------------------------------+------------+------------+------------+------------+
485 ```
486
487 You can also use this option with the `--output-format` one:
488
489 ```bash
490 $ rustdoc src/lib.rs -Z unstable-options --show-coverage --output-format json
491 {"lib.rs":{"total":4,"with_docs":4,"total_examples":4,"with_examples":1}}
492 ```
493
494 To be noted: the computation of code examples follows these rules:
495
496 1. These items aren't accounted by default:
497   * struct/union field
498   * enum variant
499   * constant
500   * static
501   * typedef
502 2. If one of the previously listed items has a code example, then it'll be counted.