]> git.proxmox.com Git - rustc.git/blame - src/doc/rustdoc/src/the-doc-attribute.md
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / doc / rustdoc / src / the-doc-attribute.md
CommitLineData
3b2f2976
XL
1# The `#[doc]` attribute
2
3The `#[doc]` attribute lets you control various aspects of how `rustdoc` does
2c00a5a8 4its job.
3b2f2976
XL
5
6The most basic function of `#[doc]` is to handle the actual documentation
7text. That is, `///` is syntax sugar for `#[doc]`. This means that these two
8are the same:
9
6a06907d 10```rust,no_run
3b2f2976
XL
11/// This is a doc comment.
12#[doc = " This is a doc comment."]
6a06907d 13# fn f() {}
3b2f2976
XL
14```
15
16(Note the leading space in the attribute version.)
17
18In most cases, `///` is easier to use than `#[doc]`. One case where the latter is easier is
19when generating documentation in macros; the `collapse-docs` pass will combine multiple
20`#[doc]` attributes into a single doc comment, letting you generate code like this:
21
6a06907d 22```rust,no_run
3b2f2976
XL
23#[doc = "This is"]
24#[doc = " a "]
25#[doc = "doc comment"]
6a06907d 26# fn f() {}
3b2f2976
XL
27```
28
29Which can feel more flexible. Note that this would generate this:
30
6a06907d 31```rust,no_run
3b2f2976 32#[doc = "This is\n a \ndoc comment"]
6a06907d 33# fn f() {}
3b2f2976
XL
34```
35
36but given that docs are rendered via Markdown, it will remove these newlines.
37
17df50a5
XL
38Another use case is for including external files as documentation:
39
40```rust,no_run
41#[doc = include_str!("../README.md")]
42# fn f() {}
43```
44
3b2f2976
XL
45The `doc` attribute has more options though! These don't involve the text of
46the output, but instead, various aspects of the presentation of the output.
47We've split them into two kinds below: attributes that are useful at the
48crate level, and ones that are useful at the item level.
49
50## At the crate level
51
74b04a01 52These options control how the docs look at a crate level.
3b2f2976
XL
53
54### `html_favicon_url`
55
56This form of the `doc` attribute lets you control the favicon of your docs.
57
6a06907d 58```rust,no_run
3b2f2976
XL
59#![doc(html_favicon_url = "https://example.com/favicon.ico")]
60```
61
62This will put `<link rel="shortcut icon" href="{}">` into your docs, where
63the string for the attribute goes into the `{}`.
64
65If you don't use this attribute, there will be no favicon.
66
67### `html_logo_url`
68
69This form of the `doc` attribute lets you control the logo in the upper
70left hand side of the docs.
71
6a06907d 72```rust,no_run
3b2f2976
XL
73#![doc(html_logo_url = "https://example.com/logo.jpg")]
74```
75
76This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into
77your docs, where the string for the attribute goes into the `{}`.
78
79If you don't use this attribute, there will be no logo.
80
81### `html_playground_url`
82
83This form of the `doc` attribute lets you control where the "run" buttons
84on your documentation examples make requests to.
85
6a06907d 86```rust,no_run
3b2f2976
XL
87#![doc(html_playground_url = "https://playground.example.com/")]
88```
89
90Now, when you press "run", the button will make a request to this domain.
91
92If you don't use this attribute, there will be no run buttons.
93
94### `issue_tracker_base_url`
95
96This form of the `doc` attribute is mostly only useful for the standard library;
97When a feature is unstable, an issue number for tracking the feature must be
98given. `rustdoc` uses this number, plus the base URL given here, to link to
99the tracking issue.
100
6a06907d 101```rust,no_run
3b2f2976
XL
102#![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
103```
104
48663c56
XL
105### `html_root_url`
106
107The `#[doc(html_root_url = "…")]` attribute value indicates the URL for
108generating links to external crates. When rustdoc needs to generate a link to
109an item in an external crate, it will first check if the extern crate has been
110documented locally on-disk, and if so link directly to it. Failing that, it
111will use the URL given by the `--extern-html-root-url` command-line flag if
112available. If that is not available, then it will use the `html_root_url`
113value in the extern crate if it is available. If that is not available, then
114the extern items will not be linked.
115
6a06907d 116```rust,no_run
48663c56
XL
117#![doc(html_root_url = "https://docs.rs/serde/1.0")]
118```
119
3b2f2976
XL
120### `html_no_source`
121
122By default, `rustdoc` will include the source code of your program, with links
123to it in the docs. But if you include this:
124
6a06907d 125```rust,no_run
3b2f2976
XL
126#![doc(html_no_source)]
127```
128
129it will not.
130
abe05a73
XL
131### `test(no_crate_inject)`
132
133By default, `rustdoc` will automatically add a line with `extern crate my_crate;` into each doctest.
134But if you include this:
135
6a06907d 136```rust,no_run
abe05a73
XL
137#![doc(test(no_crate_inject))]
138```
139
140it will not.
141
142### `test(attr(...))`
143
144This form of the `doc` attribute allows you to add arbitrary attributes to all your doctests. For
145example, if you want your doctests to fail if they produce any warnings, you could add this:
146
6a06907d 147```rust,no_run
abe05a73
XL
148#![doc(test(attr(deny(warnings))))]
149```
150
3b2f2976
XL
151## At the item level
152
153These forms of the `#[doc]` attribute are used on individual items, to control how
154they are documented.
155
156## `#[doc(no_inline)]`/`#[doc(inline)]`
157
158These attributes are used on `use` statements, and control where the documentation shows
159up. For example, consider this Rust code:
160
6a06907d 161```rust,no_run
3b2f2976
XL
162pub use bar::Bar;
163
164/// bar docs
165pub mod bar {
166 /// the docs for Bar
167 pub struct Bar;
168}
6a06907d 169# fn main() {}
3b2f2976
XL
170```
171
2c00a5a8 172The documentation will generate a "Re-exports" section, and say `pub use bar::Bar;`, where
3b2f2976
XL
173`Bar` is a link to its page.
174
175If we change the `use` line like this:
176
6a06907d 177```rust,no_run
3b2f2976
XL
178#[doc(inline)]
179pub use bar::Bar;
6a06907d
XL
180# pub mod bar { pub struct Bar; }
181# fn main() {}
3b2f2976
XL
182```
183
184Instead, `Bar` will appear in a `Structs` section, just like `Bar` was defined at the
185top level, rather than `pub use`'d.
186
187Let's change our original example, by making `bar` private:
188
6a06907d 189```rust,no_run
3b2f2976
XL
190pub use bar::Bar;
191
192/// bar docs
193mod bar {
194 /// the docs for Bar
195 pub struct Bar;
196}
6a06907d 197# fn main() {}
3b2f2976
XL
198```
199
200Here, because `bar` is not public, `Bar` wouldn't have its own page, so there's nowhere
201to link to. `rustdoc` will inline these definitions, and so we end up in the same case
202as the `#[doc(inline)]` above; `Bar` is in a `Structs` section, as if it were defined at
203the top level. If we add the `no_inline` form of the attribute:
204
6a06907d 205```rust,no_run
3b2f2976
XL
206#[doc(no_inline)]
207pub use bar::Bar;
208
209/// bar docs
210mod bar {
211 /// the docs for Bar
212 pub struct Bar;
213}
6a06907d 214# fn main() {}
3b2f2976
XL
215```
216
2c00a5a8 217Now we'll have a `Re-exports` line, and `Bar` will not link to anywhere.
3b2f2976 218
13cf67c4 219One special case: In Rust 2018 and later, if you `pub use` one of your dependencies, `rustdoc` will
dc9dc135 220not eagerly inline it as a module unless you add `#[doc(inline)]`.
13cf67c4 221
3b2f2976
XL
222## `#[doc(hidden)]`
223
224Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless
225the `strip-hidden` pass is removed.