1 # The `#[doc]` attribute
3 The `#[doc]` attribute lets you control various aspects of how `rustdoc` does
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
11 /// This is a doc comment.
12 #[doc = " This is a doc comment."]
15 (Note the leading space in the attribute version.)
17 In most cases, `///` is easier to use than `#[doc]`. One case where the latter is easier is
18 when generating documentation in macros; the `collapse-docs` pass will combine multiple
19 `#[doc]` attributes into a single doc comment, letting you generate code like this:
24 #[doc = "doc comment"]
27 Which can feel more flexible. Note that this would generate this:
30 #[doc = "This is\n a \ndoc comment"]
33 but given that docs are rendered via Markdown, it will remove these newlines.
35 The `doc` attribute has more options though! These don't involve the text of
36 the output, but instead, various aspects of the presentation of the output.
37 We've split them into two kinds below: attributes that are useful at the
38 crate level, and ones that are useful at the item level.
42 These options control how the docs look at a macro level.
44 ### `html_favicon_url`
46 This form of the `doc` attribute lets you control the favicon of your docs.
49 #![doc(html_favicon_url = "https://example.com/favicon.ico")]
52 This will put `<link rel="shortcut icon" href="{}">` into your docs, where
53 the string for the attribute goes into the `{}`.
55 If you don't use this attribute, there will be no favicon.
59 This form of the `doc` attribute lets you control the logo in the upper
60 left hand side of the docs.
63 #![doc(html_logo_url = "https://example.com/logo.jpg")]
66 This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into
67 your docs, where the string for the attribute goes into the `{}`.
69 If you don't use this attribute, there will be no logo.
71 ### `html_playground_url`
73 This form of the `doc` attribute lets you control where the "run" buttons
74 on your documentation examples make requests to.
77 #![doc(html_playground_url = "https://playground.example.com/")]
80 Now, when you press "run", the button will make a request to this domain.
82 If you don't use this attribute, there will be no run buttons.
84 ### `issue_tracker_base_url`
86 This form of the `doc` attribute is mostly only useful for the standard library;
87 When a feature is unstable, an issue number for tracking the feature must be
88 given. `rustdoc` uses this number, plus the base URL given here, to link to
92 #![doc(issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/")]
97 By default, `rustdoc` will include the source code of your program, with links
98 to it in the docs. But if you include this:
101 #![doc(html_no_source)]
106 ### `test(no_crate_inject)`
108 By default, `rustdoc` will automatically add a line with `extern crate my_crate;` into each doctest.
109 But if you include this:
112 #![doc(test(no_crate_inject))]
117 ### `test(attr(...))`
119 This form of the `doc` attribute allows you to add arbitrary attributes to all your doctests. For
120 example, if you want your doctests to fail if they produce any warnings, you could add this:
123 #![doc(test(attr(deny(warnings))))]
128 These forms of the `#[doc]` attribute are used on individual items, to control how
131 ## `#[doc(no_inline)]`/`#[doc(inline)]`
133 These attributes are used on `use` statements, and control where the documentation shows
134 up. For example, consider this Rust code:
146 The documentation will generate a "Re-exports" section, and say `pub use bar::Bar;`, where
147 `Bar` is a link to its page.
149 If we change the `use` line like this:
156 Instead, `Bar` will appear in a `Structs` section, just like `Bar` was defined at the
157 top level, rather than `pub use`'d.
159 Let's change our original example, by making `bar` private:
171 Here, because `bar` is not public, `Bar` wouldn't have its own page, so there's nowhere
172 to link to. `rustdoc` will inline these definitions, and so we end up in the same case
173 as the `#[doc(inline)]` above; `Bar` is in a `Structs` section, as if it were defined at
174 the top level. If we add the `no_inline` form of the attribute:
187 Now we'll have a `Re-exports` line, and `Bar` will not link to anywhere.
189 One special case: In Rust 2018 and later, if you `pub use` one of your dependencies, `rustdoc` will
190 not eagerly inline it as a module unless you add `#[doc(inline)}`.
194 Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless
195 the `strip-hidden` pass is removed.
197 ## `#[doc(primitive)]`
199 Since primitive types are defined in the compiler, there's no place to attach documentation
200 attributes. This attribute is used by the standard library to provide a way to generate
201 documentation for primitive types.