]> git.lizzy.rs Git - rust.git/blob - src/doc/rustdoc/src/unstable-features.md
Rollup merge of #90420 - GuillaumeGomez:rustdoc-internals-feature, r=camelid
[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 ## Extensions to the `#[doc]` attribute
42
43 These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
44 and enabled with a `#![feature(...)]` attribute in your crate.
45
46 ### `#[doc(cfg)]`: Recording what platforms or features are required for code to be present
47
48 You can use `#[doc(cfg(...))]` to tell Rustdoc exactly which platform items appear on.
49 This has two effects:
50
51 1. doctests will only run on the appropriate platforms, and
52 2. When Rustdoc renders documentation for that item, it will be accompanied by a banner explaining
53    that the item is only available on certain platforms.
54
55 `#[doc(cfg)]` is intended to be used alongside [`#[cfg(doc)]`][cfg-doc].
56 For example, `#[cfg(any(windows, doc))]` will preserve the item either on Windows or during the
57 documentation process. Then, adding a new attribute `#[doc(cfg(windows))]` will tell Rustdoc that
58 the item is supposed to be used on Windows. For example:
59
60 ```rust
61 #![feature(doc_cfg)]
62
63 /// Token struct that can only be used on Windows.
64 #[cfg(any(windows, doc))]
65 #[doc(cfg(windows))]
66 pub struct WindowsToken;
67
68 /// Token struct that can only be used on Unix.
69 #[cfg(any(unix, doc))]
70 #[doc(cfg(unix))]
71 pub struct UnixToken;
72
73 /// Token struct that is only available with the `serde` feature
74 #[cfg(feature = "serde")]
75 #[doc(cfg(feature = "serde"))]
76 #[derive(serde::Deserialize)]
77 pub struct SerdeToken;
78 ```
79
80 In this sample, the tokens will only appear on their respective platforms, but they will both appear
81 in documentation.
82
83 `#[doc(cfg(...))]` was introduced to be used by the standard library and currently requires the
84 `#![feature(doc_cfg)]` feature gate. For more information, see [its chapter in the Unstable
85 Book][unstable-doc-cfg] and [its tracking issue][issue-doc-cfg].
86
87 [cfg-doc]: ./advanced-features.md
88 [unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
89 [issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
90
91 ### Adding your trait to the "Notable traits" dialog
92
93 Rustdoc keeps a list of a few traits that are believed to be "fundamental" to
94 types that implement them. These traits are intended to be the primary interface
95 for their implementers, and are often most of the API available to be documented
96 on their types. For this reason, Rustdoc will track when a given type implements
97 one of these traits and call special attention to it when a function returns one
98 of these types. This is the "Notable traits" dialog, accessible as a circled `i`
99 button next to the function, which, when clicked, shows the dialog.
100
101 In the standard library, some of the traits that are part of this list are
102 `Iterator`, `Future`, `io::Read`, and `io::Write`. However, rather than being
103 implemented as a hard-coded list, these traits have a special marker attribute
104 on them: `#[doc(notable_trait)]`. This means that you can apply this attribute
105 to your own trait to include it in the "Notable traits" dialog in documentation.
106
107 The `#[doc(notable_trait)]` attribute currently requires the `#![feature(doc_notable_trait)]`
108 feature gate. For more information, see [its chapter in the Unstable Book][unstable-notable_trait]
109 and [its tracking issue][issue-notable_trait].
110
111 [unstable-notable_trait]: ../unstable-book/language-features/doc-notable-trait.html
112 [issue-notable_trait]: https://github.com/rust-lang/rust/issues/45040
113
114 ### Exclude certain dependencies from documentation
115
116 The standard library uses several dependencies which, in turn, use several types and traits from the
117 standard library. In addition, there are several compiler-internal crates that are not considered to
118 be part of the official standard library, and thus would be a distraction to include in
119 documentation. It's not enough to exclude their crate documentation, since information about trait
120 implementations appears on the pages for both the type and the trait, which can be in different
121 crates!
122
123 To prevent internal types from being included in documentation, the standard library adds an
124 attribute to their `extern crate` declarations: `#[doc(masked)]`. This causes Rustdoc to "mask out"
125 types from these crates when building lists of trait implementations.
126
127 The `#[doc(masked)]` attribute is intended to be used internally, and requires the
128 `#![feature(doc_masked)]` feature gate.  For more information, see [its chapter in the Unstable
129 Book][unstable-masked] and [its tracking issue][issue-masked].
130
131 [unstable-masked]: ../unstable-book/language-features/doc-masked.html
132 [issue-masked]: https://github.com/rust-lang/rust/issues/44027
133
134
135 ## Document primitives
136
137 This is for Rust compiler internal use only.
138
139 Since primitive types are defined in the compiler, there's no place to attach documentation
140 attributes. The `#[doc(primitive)]` attribute is used by the standard library to provide a way
141 to generate documentation for primitive types, and requires `#![feature(rustdoc_internals)]` to
142 enable.
143
144 ## Document keywords
145
146 This is for Rust compiler internal use only.
147
148 Rust keywords are documented in the standard library (look for `match` for example).
149
150 To do so, the `#[doc(keyword = "...")]` attribute is used. Example:
151
152 ```rust
153 #![feature(rustdoc_internals)]
154
155 /// Some documentation about the keyword.
156 #[doc(keyword = "keyword")]
157 mod empty_mod {}
158 ```
159
160 ## Unstable command-line arguments
161
162 These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are
163 themselves marked as unstable. To use any of these options, pass `-Z unstable-options` as well as
164 the flag in question to Rustdoc on the command-line. To do this from Cargo, you can either use the
165 `RUSTDOCFLAGS` environment variable or the `cargo rustdoc` command.
166
167 ### `--markdown-before-content`: include rendered Markdown before the content
168
169 Using this flag looks like this:
170
171 ```bash
172 $ rustdoc src/lib.rs -Z unstable-options --markdown-before-content extra.md
173 $ rustdoc README.md -Z unstable-options --markdown-before-content extra.md
174 ```
175
176 Just like `--html-before-content`, this allows you to insert extra content inside the `<body>` tag
177 but before the other content `rustdoc` would normally produce in the rendered documentation.
178 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
179 Markdown renderer before inserting the result into the file.
180
181 ### `--markdown-after-content`: include rendered Markdown after the content
182
183 Using this flag looks like this:
184
185 ```bash
186 $ rustdoc src/lib.rs -Z unstable-options --markdown-after-content extra.md
187 $ rustdoc README.md -Z unstable-options --markdown-after-content extra.md
188 ```
189
190 Just like `--html-after-content`, this allows you to insert extra content before the `</body>` tag
191 but after the other content `rustdoc` would normally produce in the rendered documentation.
192 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
193 Markdown renderer before inserting the result into the file.
194
195 ### `--playground-url`: control the location of the playground
196
197 Using this flag looks like this:
198
199 ```bash
200 $ rustdoc src/lib.rs -Z unstable-options --playground-url https://play.rust-lang.org/
201 ```
202
203 When rendering a crate's docs, this flag gives the base URL of the Rust Playground, to use for
204 generating `Run` buttons. Unlike `--markdown-playground-url`, this argument works for standalone
205 Markdown files *and* Rust crates. This works the same way as adding `#![doc(html_playground_url =
206 "url")]` to your crate root, as mentioned in [the chapter about the `#[doc]`
207 attribute][doc-playground]. Please be aware that the official Rust Playground at
208 https://play.rust-lang.org does not have every crate available, so if your examples require your
209 crate, make sure the playground you provide has your crate available.
210
211 [doc-playground]: the-doc-attribute.html#html_playground_url
212
213 If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone
214 Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both
215 `--playground-url` and `#![doc(html_playground_url = "url")]` are present when rendering crate docs,
216 the attribute will take precedence.
217
218 ### `--sort-modules-by-appearance`: control how items on module pages are sorted
219
220 Using this flag looks like this:
221
222 ```bash
223 $ rustdoc src/lib.rs -Z unstable-options --sort-modules-by-appearance
224 ```
225
226 Ordinarily, when `rustdoc` prints items in module pages, it will sort them alphabetically (taking
227 some consideration for their stability, and names that end in a number). Giving this flag to
228 `rustdoc` will disable this sorting and instead make it print the items in the order they appear in
229 the source.
230
231 ### `--show-type-layout`: add a section to each type's docs describing its memory layout
232
233 Using this flag looks like this:
234
235 ```bash
236 $ rustdoc src/lib.rs -Z unstable-options --show-type-layout
237 ```
238
239 When this flag is passed, rustdoc will add a "Layout" section at the bottom of
240 each type's docs page that includes a summary of the type's memory layout as
241 computed by rustc. For example, rustdoc will show the size in bytes that a value
242 of that type will take in memory.
243
244 Note that most layout information is **completely unstable** and may even differ
245 between compilations.
246
247 ### `--resource-suffix`: modifying the name of CSS/JavaScript in crate docs
248
249 Using this flag looks like this:
250
251 ```bash
252 $ rustdoc src/lib.rs -Z unstable-options --resource-suffix suf
253 ```
254
255 When rendering docs, `rustdoc` creates several CSS and JavaScript files as part of the output. Since
256 all these files are linked from every page, changing where they are can be cumbersome if you need to
257 specially cache them. This flag will rename all these files in the output to include the suffix in
258 the filename. For example, `light.css` would become `light-suf.css` with the above command.
259
260 ### `--display-doctest-warnings`: display warnings when documenting or running documentation tests
261
262 Using this flag looks like this:
263
264 ```bash
265 $ rustdoc src/lib.rs -Z unstable-options --display-doctest-warnings
266 $ rustdoc --test src/lib.rs -Z unstable-options --display-doctest-warnings
267 ```
268
269 The intent behind this flag is to allow the user to see warnings that occur within their library or
270 their documentation tests, which are usually suppressed. However, [due to a
271 bug][issue-display-warnings], this flag doesn't 100% work as intended. See the linked issue for
272 details.
273
274 [issue-display-warnings]: https://github.com/rust-lang/rust/issues/41574
275
276 ### `--extern-html-root-url`: control how rustdoc links to non-local crates
277
278 Using this flag looks like this:
279
280 ```bash
281 $ rustdoc src/lib.rs -Z unstable-options --extern-html-root-url some-crate=https://example.com/some-crate/1.0.1
282 ```
283
284 Ordinarily, when rustdoc wants to link to a type from a different crate, it looks in two places:
285 docs that already exist in the output directory, or the `#![doc(doc_html_root)]` set in the other
286 crate. However, if you want to link to docs that exist in neither of those places, you can use these
287 flags to control that behavior. When the `--extern-html-root-url` flag is given with a name matching
288 one of your dependencies, rustdoc use that URL for those docs. Keep in mind that if those docs exist
289 in the output directory, those local docs will still override this flag.
290
291 ### `-Z force-unstable-if-unmarked`
292
293 Using this flag looks like this:
294
295 ```bash
296 $ rustdoc src/lib.rs -Z force-unstable-if-unmarked
297 ```
298
299 This is an internal flag intended for the standard library and compiler that applies an
300 `#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This
301 allows `rustdoc` to be able to generate documentation for the compiler crates and the standard
302 library, as an equivalent command-line argument is provided to `rustc` when building those crates.
303
304 ### `--index-page`: provide a top-level landing page for docs
305
306 This feature allows you to generate an index-page with a given markdown file. A good example of it
307 is the [rust documentation index](https://doc.rust-lang.org/nightly/index.html).
308
309 With this, you'll have a page which you can custom as much as you want at the top of your crates.
310
311 Using `index-page` option enables `enable-index-page` option as well.
312
313 ### `--enable-index-page`: generate a default index page for docs
314
315 This feature allows the generation of a default index-page which lists the generated crates.
316
317 ### `--static-root-path`: control how static files are loaded in HTML output
318
319 Using this flag looks like this:
320
321 ```bash
322 $ rustdoc src/lib.rs -Z unstable-options --static-root-path '/cache/'
323 ```
324
325 This flag controls how rustdoc links to its static files on HTML pages. If you're hosting a lot of
326 crates' docs generated by the same version of rustdoc, you can use this flag to cache rustdoc's CSS,
327 JavaScript, and font files in a single location, rather than duplicating it once per "doc root"
328 (grouping of crate docs generated into the same output directory, like with `cargo doc`). Per-crate
329 files like the search index will still load from the documentation root, but anything that gets
330 renamed with `--resource-suffix` will load from the given path.
331
332 ### `--persist-doctests`: persist doctest executables after running
333
334 Using this flag looks like this:
335
336 ```bash
337 $ rustdoc src/lib.rs --test -Z unstable-options --persist-doctests target/rustdoctest
338 ```
339
340 This flag allows you to keep doctest executables around after they're compiled or run.
341 Usually, rustdoc will immediately discard a compiled doctest after it's been tested, but
342 with this option, you can keep those binaries around for farther testing.
343
344 ### `--show-coverage`: calculate the percentage of items with documentation
345
346 Using this flag looks like this:
347
348 ```bash
349 $ rustdoc src/lib.rs -Z unstable-options --show-coverage
350 ```
351
352 It generates something like this:
353
354 ```bash
355 +-------------------------------------+------------+------------+------------+------------+
356 | File                                | Documented | Percentage |   Examples | Percentage |
357 +-------------------------------------+------------+------------+------------+------------+
358 | lib.rs                              |          4 |     100.0% |          1 |      25.0% |
359 +-------------------------------------+------------+------------+------------+------------+
360 | Total                               |          4 |     100.0% |          1 |      25.0% |
361 +-------------------------------------+------------+------------+------------+------------+
362 ```
363
364 If you want to determine how many items in your crate are documented, pass this flag to rustdoc.
365 When it receives this flag, it will count the public items in your crate that have documentation,
366 and print out the counts and a percentage instead of generating docs.
367
368 Some methodology notes about what rustdoc counts in this metric:
369
370 * Rustdoc will only count items from your crate (i.e. items re-exported from other crates don't
371   count).
372 * Docs written directly onto inherent impl blocks are not counted, even though their doc comments
373   are displayed, because the common pattern in Rust code is to write all inherent methods into the
374   same impl block.
375 * Items in a trait implementation are not counted, as those impls will inherit any docs from the
376   trait itself.
377 * By default, only public items are counted. To count private items as well, pass
378   `--document-private-items` at the same time.
379
380 Public items that are not documented can be seen with the built-in `missing_docs` lint. Private
381 items that are not documented can be seen with Clippy's `missing_docs_in_private_items` lint.
382
383 Calculating code examples follows these rules:
384
385 1. These items aren't accounted by default:
386   * struct/union field
387   * enum variant
388   * constant
389   * static
390   * typedef
391 2. If one of the previously listed items has a code example, then it'll be counted.
392
393 #### JSON output
394
395 When using `--output-format json` with this option, it will display the coverage information in
396 JSON format. For example, here is the JSON for a file with one documented item and one
397 undocumented item:
398
399 ```rust
400 /// This item has documentation
401 pub fn foo() {}
402
403 pub fn no_documentation() {}
404 ```
405
406 ```json
407 {"no_std.rs":{"total":3,"with_docs":1,"total_examples":3,"with_examples":0}}
408 ```
409
410 Note that the third item is the crate root, which in this case is undocumented.
411
412 ### `-w`/`--output-format`: output format
413
414 `--output-format json` emits documentation in the experimental
415 [JSON format](https://github.com/rust-lang/rfcs/pull/2963). `--output-format html` has no effect,
416 and is also accepted on stable toolchains.
417
418 It can also be used with `--show-coverage`. Take a look at its
419 [documentation](#--show-coverage-get-statistics-about-code-documentation-coverage) for more
420 information.
421
422 ### `--enable-per-target-ignores`: allow `ignore-foo` style filters for doctests
423
424 Using this flag looks like this:
425
426 ```bash
427 $ rustdoc src/lib.rs -Z unstable-options --enable-per-target-ignores
428 ```
429
430 This flag allows you to tag doctests with compiletest style `ignore-foo` filters that prevent
431 rustdoc from running that test if the target triple string contains foo. For example:
432
433 ```rust
434 ///```ignore-foo,ignore-bar
435 ///assert!(2 == 2);
436 ///```
437 struct Foo;
438 ```
439
440 This will not be run when the build target is `super-awesome-foo` or `less-bar-awesome`.
441 If the flag is not enabled, then rustdoc will consume the filter, but do nothing with it, and
442 the above example will be run for all targets.
443 If you want to preserve backwards compatibility for older versions of rustdoc, you can use
444
445 ```rust
446 ///```ignore,ignore-foo
447 ///assert!(2 == 2);
448 ///```
449 struct Foo;
450 ```
451
452 In older versions, this will be ignored on all targets, but on newer versions `ignore-gnu` will
453 override `ignore`.
454
455 ### `--runtool`, `--runtool-arg`: program to run tests with; args to pass to it
456
457 Using these options looks like this:
458
459 ```bash
460 $ rustdoc src/lib.rs -Z unstable-options --runtool runner --runtool-arg --do-thing --runtool-arg --do-other-thing
461 ```
462
463 These options can be used to run the doctest under a program, and also pass arguments to
464 that program. For example, if you want to run your doctests under valgrind you might run
465
466 ```bash
467 $ rustdoc src/lib.rs -Z unstable-options --runtool valgrind
468 ```
469
470 Another use case would be to run a test inside an emulator, or through a Virtual Machine.
471
472 ### `--with-examples`: include examples of uses of items as documentation
473
474 This option, combined with `--scrape-examples-target-crate` and
475 `--scrape-examples-output-path`, is used to implement the functionality in [RFC
476 #3123](https://github.com/rust-lang/rfcs/pull/3123). Uses of an item (currently
477 functions / call-sites) are found in a crate and its reverse-dependencies, and
478 then the uses are included as documentation for that item. This feature is
479 intended to be used via `cargo doc --scrape-examples`, but the rustdoc-only
480 workflow looks like:
481
482 ```bash
483 $ rustdoc examples/ex.rs -Z unstable-options \
484     --extern foobar=target/deps/libfoobar.rmeta \
485     --scrape-examples-target-crate foobar \
486     --scrape-examples-output-path output.calls
487 $ rustdoc src/lib.rs -Z unstable-options --with-examples output.calls
488 ```
489
490 First, the library must be checked to generate an `rmeta`. Then a
491 reverse-dependency like `examples/ex.rs` is given to rustdoc with the target
492 crate being documented (`foobar`) and a path to output the calls
493 (`output.calls`). Then, the generated calls file can be passed via
494 `--with-examples` to the subsequent documentation of `foobar`.