-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Document the doc attribute #43792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document the doc attribute #43792
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# The `#[doc]` attribute | ||
|
||
The `#[doc]` attribute lets you control various aspects of how `rustdoc` does | ||
its job. | ||
|
||
The most basic job of `#[doc]` is to be the way that the text of the documentation | ||
is handled. That is, `///` is syntax sugar for `#[doc]`. This means that these two | ||
are the same: | ||
|
||
```rust,ignore | ||
/// This is a doc comment. | ||
#[doc = "This is a doc comment."] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that these are not actually the same! The former has a leading space, but the latter does not. This is why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding in a note |
||
``` | ||
|
||
In most cases, `///` is easier to use than `#[doc]`. One case where the latter is easier is | ||
when generating documentation in macros; the `collapse-docs` pass will combine multiple | ||
`#[doc]` attributes into a single doc comment, letting you generate code like this: | ||
|
||
```rust,ignore | ||
#[doc = "This is"] | ||
#[doc = " a "] | ||
#[doc = "doc comment"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, and then it's markdown that makes them all in one line in the final output. whew. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh right, markdown joins those lines up anyway There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be resolved now |
||
``` | ||
|
||
Which can feel more flexible. | ||
|
||
The `doc` attribute has more options though! These don't involve the text of | ||
the output, but instead, various aspects of the presentation of the output. | ||
We've split them into two kinds below: attributes that are useful at the | ||
crate level, and ones that are useful at the item level. | ||
|
||
## At the crate level | ||
|
||
These options control how the docs look at a macro level. | ||
|
||
### `html_favicon_url` | ||
|
||
This form of the `doc` attribute lets you control the favicon of your docs. | ||
|
||
```rust,ignore | ||
#![doc(html_favicon_url = "https://foo.com/favicon.ico")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example URLs in docs you really should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
``` | ||
|
||
This will put `<link rel="shortcut icon" href="{}">` into your docs, where | ||
the string for the attribute goes into the `{}`. | ||
|
||
### `html_logo_url` | ||
|
||
This form of the `doc` attribute lets you control the logo in the upper | ||
left hand side of the docs. | ||
|
||
```rust,ignore | ||
#![doc(html_logo_url = "https://foo.com/logo.jpg")] | ||
``` | ||
|
||
This will put `<a href='index.html'><img src='{}' alt='logo' width='100'></a>` into | ||
your docs, where the string for the attribute goes into the `{}`. | ||
|
||
### `html_playground_url` | ||
|
||
This form of the `doc` attribute lets you control where the "run" buttons | ||
on your documentation examples make requests to. | ||
|
||
```rust,ignore | ||
#![doc(html_playground_url = "https://playground.foo.com/")] | ||
``` | ||
|
||
Now, when you press "run", the button will make a request to this domain. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
### `issue_tracker_base_url` | ||
|
||
This form of the `doc` attribute is mostly only useful for the standard library; | ||
When a feature is unstable, an issue number for tracking the feature must be | ||
given. `rustdoc` uses this number, plus the base URL given here, to link to | ||
the tracking issue. | ||
|
||
```rust,ignore | ||
#![doc(issue_tracker_base_url = "https://github.com/foo/foo/issues/")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again this should be replaced with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
``` | ||
|
||
### `html_no_source` | ||
|
||
By default, `rustdoc` will include the source code of your program, with links | ||
to it in the docs. But if you include this: | ||
|
||
```rust,ignore | ||
#![doc(html_no_source)] | ||
``` | ||
|
||
it will not. | ||
|
||
## At the item level | ||
|
||
These forms of the `#[doc]` attribute are used on individual items, to control how | ||
they are documented. | ||
|
||
## `#[doc(no_inline)]` | ||
|
||
## `#[doc(hidden)]` | ||
|
||
Any item annotated with `#[doc(hidden)]` will not appear in the documentation, unless | ||
the `strip-hidden` pass is removed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, this would link to the section in your other PR, but it depends which PR gets shipped first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I think we should worry about intra-linking after all the chapters land |
||
|
||
## `#[doc(primitive)]` | ||
|
||
Since primitive types are defined in the compiler, there's no place to attach documentation | ||
attributes. This attribute is used by the standard library to provide a way to generate | ||
documentation for primitive types. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When i was adding the new primitive pages, i wondered whether it would be useful for certain extension crates to include their own primitive pages, in case they implement their traits on the primitives. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hm, I think I'd want to see how this works before advocating for people to use it. Wouldn't this overwrite the usual primitive page? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What usual primitive page? Some random crate isn't going to include the libstd primitive docs anywhere. I can try this out with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider https://docs.rs/mysql/12.0.2/mysql/struct.Row.html#method.len the return type is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, the links! Yeah, that would totally overwrite those links. ...in fact, it would probably also overwrite those links in any dependent crates, too. I think the thing that populates those links searches from the top down, which means that any overridden links would definitely appear before libstd. Possibly worth investigating, and maybe worth changing, but it's probably not something to encourage in that case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this phrasing (and the repetition of "job") really awkward... how about "The most basic function of
#[doc]
is to handle the actual documentation text."?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good to me!