Skip to content

Commit edd2a7e

Browse files
authored
Merge pull request #1323 from jsjoeio/jsjoeio/108-doc-attribute-examples
feat: add doc attributes section to documentation
2 parents 325a512 + 5382d4b commit edd2a7e

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/meta/doc.md

+49-10
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Use `cargo test` to run all tests (including documentation tests), and `cargo te
66

77
These commands will appropriately invoke `rustdoc` (and `rustc`) as required.
88

9-
### Doc comments
9+
## Doc comments
1010

1111
Doc comments are very useful for big projects that require documentation. When
1212
running `rustdoc`, these are the comments that get compiled into
1313
documentation. They are denoted by a `///`, and support [Markdown].
1414

15-
```rust,editable,ignore
15+
````rust,editable,ignore
1616
#![crate_name = "doc"]
1717
1818
/// A human being is represented here
@@ -55,7 +55,7 @@ fn main() {
5555
5656
john.hello();
5757
}
58-
```
58+
````
5959

6060
To run the tests, first build the code as a library, then tell `rustdoc` where
6161
to find the library so it can link it into each doctest program:
@@ -65,18 +65,57 @@ $ rustc doc.rs --crate-type lib
6565
$ rustdoc --test --extern doc="libdoc.rlib" doc.rs
6666
```
6767

68+
## Doc attributes
69+
70+
Below are a few examples of the most common `#[doc]` attributes used with `rustdoc`.
71+
72+
### `inline`
73+
74+
Used to inline docs, instead of linking out to separate page.
75+
76+
```rust,ignore
77+
#[doc(inline)]
78+
pub use bar::Bar;
79+
80+
/// bar docs
81+
mod bar {
82+
/// the docs for Bar
83+
pub struct Bar;
84+
}
85+
```
86+
87+
### `no_inline`
88+
89+
Used to prevent linking out to separate page or anywhere.
90+
91+
```rust,ignore
92+
// Example from libcore/prelude
93+
#[doc(no_inline)]
94+
pub use crate::mem::drop;
95+
```
96+
97+
### `hidden`
98+
99+
Using this tells `rustdoc` not to include this in documentation:
100+
101+
```rust,editable,ignore
102+
// Example from the futures-rs library
103+
#[doc(hidden)]
104+
pub use self::async_await::*;
105+
```
106+
68107
For documentation, `rustdoc` is widely used by the community. It's what is used to generate the [std library docs](https://doc.rust-lang.org/std/).
69108

70109
### See also:
71110

72-
* [The Rust Book: Making Useful Documentation Comments][book]
73-
* [The rustdoc Book][rustdoc-book]
74-
* [The Reference: Doc comments][ref-comments]
75-
* [RFC 1574: API Documentation Conventions][api-conv]
76-
* [RFC 1946: Relative links to other items from doc comments (intra-rustdoc links)][intra-links]
77-
* [Is there any documentation style guide for comments? (reddit)][reddit]
111+
- [The Rust Book: Making Useful Documentation Comments][book]
112+
- [The rustdoc Book][rustdoc-book]
113+
- [The Reference: Doc comments][ref-comments]
114+
- [RFC 1574: API Documentation Conventions][api-conv]
115+
- [RFC 1946: Relative links to other items from doc comments (intra-rustdoc links)][intra-links]
116+
- [Is there any documentation style guide for comments? (reddit)][reddit]
78117

79-
[Markdown]: https://en.wikipedia.org/wiki/Markdown
118+
[markdown]: https://en.wikipedia.org/wiki/Markdown
80119
[book]: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html#making-useful-documentation-comments
81120
[ref-comments]: https://doc.rust-lang.org/stable/reference/comments.html#doc-comments
82121
[rustdoc-book]: https://doc.rust-lang.org/rustdoc/index.html

src/meta/playpen.md

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ This allows the reader to both run your code sample, but also modify and tweak i
2020
```
2121
````
2222

23+
Additionally, you can add `ignore` if you want `mdbook` to skip your code when it builds and tests.
24+
25+
````markdown
26+
```rust,editable,ignore
27+
//...place your code here
28+
```
29+
````
30+
2331
## Using it with docs
2432

2533
You may have noticed in some of the [official Rust docs][official-rust-docs] a button that says "Run", which opens the code sample up in a new tab in Rust Playground. This feature is enabled if you use the #[doc] attribute called [`html_playground_url`][html-playground-url].

0 commit comments

Comments
 (0)