Skip to content

Commit 99e943c

Browse files
committed
Document the doc attribute
cc rust-lang#42322
1 parent b617960 commit 99e943c

File tree

3 files changed

+109
-4
lines changed

3 files changed

+109
-4
lines changed

src/doc/rustdoc/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- [What is rustdoc?](what-is-rustdoc.md)
44
- [Command-line arguments](command-line-arguments.md)
5-
- [In-source directives](in-source-directives.md)
5+
- [The `#[doc]` attribute](the-doc-attribute.md)
66
- [Documentation tests](documentation-tests.md)
77
- [Plugins](plugins.md)
88
- [Passes](passes.md)

src/doc/rustdoc/src/in-source-directives.md

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# The `#[doc]` attribute
2+
3+
The `#[doc]` attribute lets you control various aspects of how `rustdoc` does
4+
its job.
5+
6+
The most basic job of `#[doc]` is to be the way that the text of the documentation
7+
is handled. That is, `///` is syntax sugar for `#[doc]`. This means that these two
8+
are the same:
9+
10+
```rust,ignore
11+
/// This is a doc comment.
12+
#[doc = "This is a doc comment."]
13+
```
14+
15+
In most cases, `///` is easier to use than `#[doc]`. One case where the latter is easier is
16+
when generating documentation in macros; the `collapse-docs` pass will combine multiple
17+
`#[doc]` attributes into a single doc comment, letting you generate code like this:
18+
19+
```rust,ignore
20+
#[doc = "This is"]
21+
#[doc = " a "]
22+
#[doc = "doc comment"]
23+
```
24+
25+
Which can feel more flexible.
26+
27+
The `doc` attribute has more options though! These don't involve the text of
28+
the output, but instead, various aspects of the presentation of the output.
29+
We've split them into two kinds below: attributes that are useful at the
30+
crate level, and ones that are useful at the item level.
31+
32+
## At the crate level
33+
34+
These options control how the docs look at a macro level.
35+
36+
### `html_favicon_url`
37+
38+
This form of the `doc` attribute lets you control the favicon of your docs.
39+
40+
```rust,ignore
41+
#![doc(html_favicon_url = "https://foo.com/favicon.ico")]
42+
```
43+
44+
This will put `<link rel="shortcut icon" href="{}">` into your docs, where
45+
the string for the attribute goes into the `{}`.
46+
47+
### `html_logo_url`
48+
49+
This form of the `doc` attribute lets you control the logo in the upper
50+
left hand side of the docs.
51+
52+
```rust,ignore
53+
#![doc(html_logo_url = "https://foo.com/logo.jpg")]
54+
```
55+
56+
This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into
57+
your docs, where the string for the attribute goes into the `{}`.
58+
59+
### `html_playground_url`
60+
61+
This form of the `doc` attribute lets you control where the "run" buttons
62+
on your documentation examples make requests to.
63+
64+
```rust,ignore
65+
#![doc(html_playground_url = "https://playground.foo.com/")]
66+
```
67+
68+
Now, when you press "run", the button will make a request to this domain.
69+
70+
### `issue_tracker_base_url`
71+
72+
This form of the `doc` attribute is mostly only useful for the standard library;
73+
When a feature is unstable, an issue number for tracking the feature must be
74+
given. `rustdoc` uses this number, plus the base URL given here, to link to
75+
the tracking issue.
76+
77+
```rust,ignore
78+
#![doc(issue_tracker_base_url = "https://github.com/foo/foo/issues/")]
79+
```
80+
81+
### `html_no_source`
82+
83+
By default, `rustdoc` will include the source code of your program, with links
84+
to it in the docs. But if you include this:
85+
86+
```rust,ignore
87+
#![doc(html_no_source)]
88+
```
89+
90+
it will not.
91+
92+
## At the item level
93+
94+
These forms of the `#[doc]` attribute are used on individual items, to control how
95+
they are documented.
96+
97+
## `#[doc(no_inline)]`
98+
99+
## `#[doc(hidden)]`
100+
101+
Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless
102+
the `strip-hidden` pass is removed.
103+
104+
## `#[doc(primitive)]`
105+
106+
Since primitive types are defined in the compiler, there's no place to attach documentation
107+
attributes. This attribute is used by the standard library to provide a way to generate
108+
documentation for primitive types.

0 commit comments

Comments
 (0)