]> git.lizzy.rs Git - rust.git/blob - src/doc/rustdoc.md
doc: Document the new #[doc(no_inline)] attribute
[rust.git] / src / doc / rustdoc.md
1 % Rust Documentation
2
3 `rustdoc` is the built-in tool for generating documentation. It integrates
4 with the compiler to provide accurate hyperlinking between usage of types and
5 their documentation. Furthermore, by not using a separate parser, it will
6 never reject your valid Rust code.
7
8 # Creating Documentation
9
10 Documenting Rust APIs is quite simple. To document a given item, we have "doc
11 comments":
12
13 ~~~
14 # #![allow(unused_attribute)]
15 // the "link" crate attribute is currently required for rustdoc, but normally
16 // isn't needed.
17 #![crate_id = "universe"]
18 #![crate_type="lib"]
19
20 //! Tools for dealing with universes (this is a doc comment, and is shown on
21 //! the crate index page. The ! makes it apply to the parent of the comment,
22 //! rather than what follows).
23
24 # mod workaround_the_outer_function_rustdoc_inserts {
25 /// Widgets are very common (this is a doc comment, and will show up on
26 /// Widget's documentation).
27 pub struct Widget {
28         /// All widgets have a purpose (this is a doc comment, and will show up
29         /// the field's documentation).
30         purpose: String,
31         /// Humans are not allowed to understand some widgets
32         understandable: bool
33 }
34
35 pub fn recalibrate() {
36         //! Recalibrate a pesky universe (this is also a doc comment, like above,
37         //! the documentation will be applied to the *parent* item, so
38         //! `recalibrate`).
39         /* ... */
40 }
41 # }
42 ~~~
43
44 Documentation can also be controlled via the `doc` attribute on items.
45
46 ~~~
47 #[doc = "
48 Calculates the factorial of a number.
49
50 Given the input integer `n`, this function will calculate `n!` and return it.
51 "]
52 pub fn factorial(n: int) -> int { if n < 2 {1} else {n * factorial(n)} }
53 ~~~
54
55 The `doc` attribute can also be used to control how rustdoc emits documentation
56 in some cases.
57
58 ```
59 // Rustdoc will inline documentation of a `pub use` into this crate when the
60 // `pub use` reaches across crates, but this behavior can also be disabled.
61 #[doc(no_inline)]
62 pub use std::option::Option;
63 ```
64
65 Doc comments are markdown, and are currently parsed with the
66 [sundown][sundown] library. rustdoc does not yet do any fanciness such as
67 referencing other items inline, like javadoc's `@see`. One exception to this
68 is that the first paragraph will be used as the "summary" of an item in the
69 generated documentation:
70
71 ~~~
72 /// A whizbang. Does stuff. (this line is the summary)
73 ///
74 /// Whizbangs are ...
75 struct Whizbang;
76 ~~~
77
78 To generate the docs, run `rustdoc universe.rs`. By default, it generates a
79 directory called `doc`, with the documentation for `universe` being in
80 `doc/universe/index.html`. If you are using other crates with `extern crate`,
81 rustdoc will even link to them when you use their types, as long as their
82 documentation has already been generated by a previous run of rustdoc, or the
83 crate advertises that its documentation is hosted at a given URL.
84
85 The generated output can be controlled with the `doc` crate attribute, which
86 is how the above advertisement works. An example from the `libstd`
87 documentation:
88
89 ~~~
90 #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
91       html_favicon_url = "http://www.rust-lang.org/favicon.ico",
92       html_root_url = "http://doc.rust-lang.org/")];
93 ~~~
94
95 The `html_root_url` is the prefix that rustdoc will apply to any references to
96 that crate's types etc.
97
98 rustdoc can also generate JSON, for consumption by other tools, with
99 `rustdoc --output-format json`, and also consume already-generated JSON with
100 `rustdoc --input-format json`.
101
102 # Using the Documentation
103
104 The web pages generated by rustdoc present the same logical hierarchy that one
105 writes a library with. Every kind of item (function, struct, etc) has its own
106 color, and one can always click on a colored type to jump to its
107 documentation. There is a search bar at the top, which is powered by some
108 JavaScript and a statically-generated search index. No special web server is
109 required for the search.
110
111 [sundown]: https://github.com/vmg/sundown/
112
113 # Testing the Documentation
114
115 `rustdoc` has support for testing code examples which appear in the
116 documentation. This is helpful for keeping code examples up to date with the
117 source code.
118
119 To test documentation, the `--test` argument is passed to rustdoc:
120
121 ~~~ {.notrust}
122 rustdoc --test crate.rs
123 ~~~
124
125 ## Defining tests
126
127 Rust documentation currently uses the markdown format, and rustdoc treats all
128 code blocks as testable-by-default. In order to not run a test over a block of
129 code, the `ignore` string can be added to the three-backtick form of markdown
130 code block.
131
132 ~~~notrust
133 ```
134 // This is a testable code block
135 ```
136
137 ```ignore
138 // This is not a testable code block
139 ```
140
141     // This is a testable code block (4-space indent)
142 ~~~
143
144 You can specify that the test's execution should fail with the `should_fail`
145 directive.
146
147 ~~~notrust
148 ```should_fail
149 // This code block is expected to generate a failure when run
150 ```
151 ~~~
152
153 You can specify that the code block should be compiled but not run with the
154 `no_run` directive.
155
156 ~~~notrust
157 ```no_run
158 // This code will be compiled but not executed
159 ```
160 ~~~
161
162 Rustdoc also supplies some extra sugar for helping with some tedious
163 documentation examples. If a line is prefixed with `# `, then the line
164 will not show up in the HTML documentation, but it will be used when
165 testing the code block (NB. the space after the `#` is required, so
166 that one can still write things like `#[deriving(Eq)]`).
167
168 ~~~notrust
169 ```
170 # /!\ The three following lines are comments, which are usually stripped off by
171 # the doc-generating tool.  In order to display them anyway in this particular
172 # case, the character following the leading '#' is not a usual space like in
173 # these first five lines but a non breakable one.
174 # // showing 'fib' in this documentation would just be tedious and detracts from
175 # // what's actually being documented.
176 # fn fib(n: int) { n + 2 }
177
178 spawn(proc() { fib(200); })
179 ```
180 ~~~
181
182 The documentation online would look like `spawn(proc() { fib(200); })`, but when
183 testing this code, the `fib` function will be included (so it can compile).
184
185 ## Running tests (advanced)
186
187 Running tests often requires some special configuration to filter tests, find
188 libraries, or try running ignored examples. The testing framework that rustdoc
189 uses is build on crate `test`, which is also used when you compile crates with
190 rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness
191 with the `--test-args` flag.
192
193 ~~~ {.notrust}
194 # Only run tests containing 'foo' in their name
195 $ rustdoc --test lib.rs --test-args 'foo'
196
197 # See what's possible when running tests
198 $ rustdoc --test lib.rs --test-args '--help'
199 ~~~
200
201 When testing a library, code examples will often show how functions are used,
202 and this code often requires `use`-ing paths from the crate. To accommodate this,
203 rustdoc will implicitly add `extern crate <crate>;` where `<crate>` is the name of
204 the crate being tested to the top of each code example. This means that rustdoc
205 must be able to find a compiled version of the library crate being tested. Extra
206 search paths may be added via the `-L` flag to `rustdoc`.
207
208 # Standalone Markdown files
209
210 As well as Rust crates, rustdoc supports rendering pure Markdown files
211 into HTML and testing the code snippets from them. A Markdown file is
212 detected by a `.md` or `.markdown` extension.
213
214 There are 4 options to modify the output that Rustdoc creates.
215
216 - `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`.
217 - `--markdown-in-header FILE`: includes the contents of `FILE` at the
218   end of the `<head>...</head>` section.
219 - `--markdown-before-content FILE`: includes the contents of `FILE`
220   directly after `<body>`, before the rendered content (including the
221   title).
222 - `--markdown-after-content FILE`: includes the contents of `FILE`
223   directly before `</body>`, after all the rendered content.
224
225 All of these can be specified multiple times, and they are output in
226 the order in which they are specified. The first line of the file must
227 be the title, prefixed with `%` (e.g. this page has `% Rust
228 Documentation` on the first line).
229
230 Like with a Rust crate, the `--test` argument will run the code
231 examples to check they compile, and obeys any `--test-args` flags. The
232 tests are named after the last `#` heading.