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