]> git.lizzy.rs Git - rust.git/blob - src/doc/rustdoc/src/unstable-features.md
9cb5760ec3797c201750950ebbc0456baef84e76
[rust.git] / src / doc / rustdoc / src / unstable-features.md
1 # Unstable features
2
3 Rustdoc is under active developement, and like the Rust compiler, some features are only available
4 on the nightly releases. Some of these are new and need some more testing before they're able to get
5 released to the world at large, and some of them are tied to features in the Rust compiler that are
6 themselves unstable. Several features here require a matching `#![feature(...)]` attribute to
7 enable, and thus are more fully documented in the [Unstable Book]. Those sections will link over
8 there as necessary.
9
10 [Unstable Book]: ../unstable-book/index.html
11
12 ## Nightly-gated functionality
13
14 These features just require a nightly build to operate. Unlike the other features on this page,
15 these don't need to be "turned on" with a command-line flag or a `#![feature(...)]` attribute in
16 your crate. This can give them some subtle fallback modes when used on a stable release, so be
17 careful!
18
19 ### Error numbers for `compile-fail` doctests
20
21 As detailed in [the chapter on documentation tests][doctest-attributes], you can add a
22 `compile_fail` attribute to a doctest to state that the test should fail to compile. However, on
23 nightly, you can optionally add an error number to state that a doctest should emit a specific error
24 number:
25
26 [doctest-attributes]: documentation-tests.html#attributes
27
28 ``````markdown
29 ```compile_fail,E0044
30 extern { fn some_func<T>(x: T); }
31 ```
32 ``````
33
34 This is used by the error index to ensure that the samples that correspond to a given error number
35 properly emit that error code. However, these error codes aren't guaranteed to be the only thing
36 that a piece of code emits from version to version, so this in unlikely to be stabilized in the
37 future.
38
39 Attempting to use these error numbers on stable will result in the code sample being interpreted as
40 plain text.
41
42 ### Linking to items by type
43
44 As designed in [RFC 1946], Rustdoc can parse paths to items when you use them as links. To resolve
45 these type names, it uses the items currently in-scope, either by declaration or by `use` statement.
46 For modules, the "active scope" depends on whether the documentation is written outside the module
47 (as `///` comments on the `mod` statement) or inside the module (at `//!` comments inside the file
48 or block). For all other items, it uses the enclosing module's scope.
49
50 [RFC 1946]: https://github.com/rust-lang/rfcs/pull/1946
51
52 For example, in the following code:
53
54 ```rust
55 /// Does the thing.
56 pub fn do_the_thing(_: SomeType) {
57         println!("Let's do the thing!");
58 }
59
60 /// Token you use to [`do_the_thing`].
61 pub struct SomeType;
62 ```
63
64 The link to ``[`do_the_thing`]`` in `SomeType`'s docs will properly link to the page for `fn
65 do_the_thing`. Note that here, rustdoc will insert the link target for you, but manually writing the
66 target out also works:
67
68 ```rust
69 pub mod some_module {
70         /// Token you use to do the thing.
71         pub struct SomeStruct;
72 }
73
74 /// Does the thing. Requires one [`SomeStruct`] for the thing to work.
75 ///
76 /// [`SomeStruct`]: some_module::SomeStruct
77 pub fn do_the_thing(_: some_module::SomeStruct) {
78         println!("Let's do the thing!");
79 }
80 ```
81
82 For more details, check out [the RFC][RFC 1946], and see [the tracking issue][43466] for more
83 information about what parts of the feature are available.
84
85 [43466]: https://github.com/rust-lang/rust/issues/43466
86
87 ## Extensions to the `#[doc]` attribute
88
89 These features operate by extending the `#[doc]` attribute, and thus can be caught by the compiler
90 and enabled with a `#![feature(...)]` attribute in your crate.
91
92 ### Documenting platform-/feature-specific information
93
94 Because of the way Rustdoc documents a crate, the documentation it creates is specific to the target
95 rustc compiles for. Anything that's specific to any other target is dropped via `#[cfg]` attribute
96 processing early in the compilation process. However, Rustdoc has a trick up its sleeve to handle
97 platform-specific code if it *does* receive it.
98
99 Because Rustdoc doesn't need to fully compile a crate to binary, it replaces function bodies with
100 `loop {}` to prevent having to process more than necessary. This means that any code within a
101 function that requires platform-specific pieces is ignored. Combined with a special attribute,
102 `#[doc(cfg(...))]`, you can tell Rustdoc exactly which platform something is supposed to run on,
103 ensuring that doctests are only run on the appropriate platforms.
104
105 The `#[doc(cfg(...))]` attribute has another effect: When Rustdoc renders documentation for that
106 item, it will be accompanied by a banner explaining that the item is only available on certain
107 platforms.
108
109 As mentioned earlier, getting the items to Rustdoc requires some extra preparation. The standard
110 library adds a `--cfg dox` flag to every Rustdoc command, but the same thing can be accomplished by
111 adding a feature to your Cargo.toml and adding `--feature dox` (or whatever you choose to name the
112 feature) to your `cargo doc` calls.
113
114 Either way, once you create an environment for the documentation, you can start to augment your
115 `#[cfg]` attributes to allow both the target platform *and* the documentation configuration to leave
116 the item in. For example, `#[cfg(any(windows, feature = "dox"))]` will preserve the item either on
117 Windows or during the documentation process. Then, adding a new attribute `#[doc(cfg(windows))]`
118 will tell Rustdoc that the item is supposed to be used on Windows. For example:
119
120 ```rust
121 #![feature(doc_cfg)]
122
123 /// Token struct that can only be used on Windows.
124 #[cfg(any(windows, feature = "dox"))]
125 #[doc(cfg(windows))]
126 pub struct WindowsToken;
127
128 /// Token struct that can only be used on Unix.
129 #[cfg(any(unix, feature = "dox"))]
130 #[doc(cfg(unix))]
131 pub struct UnixToken;
132 ```
133
134 In this sample, the tokens will only appear on their respective platforms, but they will both appear
135 in documentation.
136
137 `#[doc(cfg(...))]` was introduced to be used by the standard library and is currently controlled by
138 a feature gate. For more information, see [its chapter in the Unstable Book][unstable-doc-cfg] and
139 [its tracking issue][issue-doc-cfg].
140
141 [unstable-doc-cfg]: ../unstable-book/language-features/doc-cfg.html
142 [issue-doc-cfg]: https://github.com/rust-lang/rust/issues/43781
143
144 ### Adding your trait to the "Important Traits" dialog
145
146 Rustdoc keeps a list of a few traits that are believed to be "fundamental" to a given type when
147 implemented on it. These traits are intended to be the primary interface for their types, and are
148 often the only thing available to be documented on their types. For this reason, Rustdoc will track
149 when a given type implements one of these traits and call special attention to it when a function
150 returns one of these types. This is the "Important Traits" dialog, visible as a circle-i button next
151 to the function, which, when clicked, shows the dialog.
152
153 In the standard library, the traits that qualify for inclusion are `Iterator`, `io::Read`, and
154 `io::Write`. However, rather than being implemented as a hard-coded list, these traits have a
155 special marker attribute on them: `#[doc(spotlight)]`. This means that you could apply this
156 attribute to your own trait to include it in the "Important Traits" dialog in documentation.
157
158 The `#[doc(spotlight)]` attribute is controlled by a feature gate. For more information, see [its
159 chapter in the Unstable Book][unstable-spotlight] and [its tracking issue][issue-spotlight].
160
161 [unstable-spotlight]: ../unstable-book/language-features/doc-spotlight.html
162 [issue-spotlight]: https://github.com/rust-lang/rust/issues/45040
163
164 ### Exclude certain dependencies from documentation
165
166 The standard library uses several dependencies which, in turn, use several types and traits from the
167 standard library. In addition, there are several compiler-internal crates that are not considered to
168 be part of the official standard library, and thus would be a distraction to include in
169 documentation. It's not enough to exclude their crate documentation, since information about trait
170 implementations appears on the pages for both the type and the trait, which can be in different
171 crates!
172
173 To prevent internal types from being included in documentation, the standard library adds an
174 attribute to their `extern crate` declarations: `#[doc(masked)]`. This causes Rustdoc to "mask out"
175 types from these crates when building lists of trait implementations.
176
177 The `#[doc(masked)]` attribute is intended to be used internally, and is controlled by a feature
178 gate.  For more information, see [its chapter in the Unstable Book][unstable-masked] and [its
179 tracking issue][issue-masked].
180
181 [unstable-masked]: ../unstable-book/language-features/doc-masked.html
182 [issue-masked]: https://github.com/rust-lang/rust/issues/44027
183
184 ### Include external files as API documentation
185
186 As designed in [RFC 1990], Rustdoc can read an external file to use as a type's documentation. This
187 is useful if certain documentation is so long that it would break the flow of reading the source.
188 Instead of writing it all inline, writing `#[doc(include = "sometype.md")]` (where `sometype.md` is
189 a file adjacent to the `lib.rs` for the crate) will ask Rustdoc to instead read that file and use it
190 as if it were written inline.
191
192 [RFC 1990]: https://github.com/rust-lang/rfcs/pull/1990
193
194 `#[doc(include = "...")]` is currently controlled by a feature gate. For more information, see [its
195 chapter in the Unstable Book][unstable-include] and [its tracking issue][issue-include].
196
197 [unstable-include]: ../unstable-book/language-features/external-doc.html
198 [issue-include]: https://github.com/rust-lang/rust/issues/44732
199
200 ## Unstable command-line arguments
201
202 These features are enabled by passing a command-line flag to Rustdoc, but the flags in question are
203 themselves marked as unstable. To use any of these options, pass `-Z unstable-options` as well as
204 the flag in question to Rustdoc on the command-line. To do this from Cargo, you can either use the
205 `RUSTDOCFLAGS` environment variable or the `cargo rustdoc` command.
206
207 ### `--markdown-before-content`: include rendered Markdown before the content
208
209 Using this flag looks like this:
210
211 ```bash
212 $ rustdoc src/lib.rs -Z unstable-options --markdown-before-content extra.md
213 $ rustdoc README.md -Z unstable-options --markdown-before-content extra.md
214 ```
215
216 Just like `--html-before-content`, this allows you to insert extra content inside the `<body>` tag
217 but before the other content `rustdoc` would normally produce in the rendered documentation.
218 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
219 Markdown renderer before inserting the result into the file.
220
221 ### `--markdown-after-content`: include rendered Markdown after the content
222
223 Using this flag looks like this:
224
225 ```bash
226 $ rustdoc src/lib.rs -Z unstable-options --markdown-after-content extra.md
227 $ rustdoc README.md -Z unstable-options --markdown-after-content extra.md
228 ```
229
230 Just like `--html-after-content`, this allows you to insert extra content before the `</body>` tag
231 but after the other content `rustdoc` would normally produce in the rendered documentation.
232 However, instead of directly inserting the file verbatim, `rustdoc` will pass the files through a
233 Markdown renderer before inserting the result into the file.
234
235 ### `--playground-url`: control the location of the playground
236
237 Using this flag looks like this:
238
239 ```bash
240 $ rustdoc src/lib.rs -Z unstable-options --playground-url https://play.rust-lang.org/
241 ```
242
243 When rendering a crate's docs, this flag gives the base URL of the Rust Playground, to use for
244 generating `Run` buttons. Unlike `--markdown-playground-url`, this argument works for standalone
245 Markdown files *and* Rust crates. This works the same way as adding `#![doc(html_playground_url =
246 "url")]` to your crate root, as mentioned in [the chapter about the `#[doc]`
247 attribute][doc-playground]. Please be aware that the official Rust Playground at
248 https://play.rust-lang.org does not have every crate available, so if your examples require your
249 crate, make sure the playground you provide has your crate available.
250
251 [doc-playground]: the-doc-attribute.html#html_playground_url
252
253 If both `--playground-url` and `--markdown-playground-url` are present when rendering a standalone
254 Markdown file, the URL given to `--markdown-playground-url` will take precedence. If both
255 `--playground-url` and `#![doc(html_playground_url = "url")]` are present when rendering crate docs,
256 the attribute will take precedence.