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.
8 # Creating Documentation
10 Documenting Rust APIs is quite simple. To document a given item, we have "doc
14 # #![allow(unused_attribute)]
15 // the "link" crate attribute is currently required for rustdoc, but normally
17 #![crate_id = "universe"]
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).
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).
28 /// All widgets have a purpose (this is a doc comment, and will show up
29 /// the field's documentation).
31 /// Humans are not allowed to understand some widgets
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
44 Doc comments are markdown, and are currently parsed with the
45 [sundown][sundown] library. rustdoc does not yet do any fanciness such as
46 referencing other items inline, like javadoc's `@see`. One exception to this
47 is that the first paragraph will be used as the "summary" of an item in the
48 generated documentation:
51 /// A whizbang. Does stuff. (this line is the summary)
57 To generate the docs, run `rustdoc universe.rs`. By default, it generates a
58 directory called `doc`, with the documentation for `universe` being in
59 `doc/universe/index.html`. If you are using other crates with `extern crate`,
60 rustdoc will even link to them when you use their types, as long as their
61 documentation has already been generated by a previous run of rustdoc, or the
62 crate advertises that its documentation is hosted at a given URL.
64 The generated output can be controlled with the `doc` crate attribute, which
65 is how the above advertisement works. An example from the `libstd`
69 #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
70 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
71 html_root_url = "http://doc.rust-lang.org/")];
74 The `html_root_url` is the prefix that rustdoc will apply to any references to
75 that crate's types etc.
77 rustdoc can also generate JSON, for consumption by other tools, with
78 `rustdoc --output-format json`, and also consume already-generated JSON with
79 `rustdoc --input-format json`.
81 # Using the Documentation
83 The web pages generated by rustdoc present the same logical hierarchy that one
84 writes a library with. Every kind of item (function, struct, etc) has its own
85 color, and one can always click on a colored type to jump to its
86 documentation. There is a search bar at the top, which is powered by some
87 JavaScript and a statically-generated search index. No special web server is
88 required for the search.
90 [sundown]: https://github.com/vmg/sundown/
92 # Testing the Documentation
94 `rustdoc` has support for testing code examples which appear in the
95 documentation. This is helpful for keeping code examples up to date with the
98 To test documentation, the `--test` argument is passed to rustdoc:
101 rustdoc --test crate.rs
106 Rust documentation currently uses the markdown format, and rustdoc treats all
107 code blocks as testable-by-default. In order to not run a test over a block of
108 code, the `ignore` string can be added to the three-backtick form of markdown
113 // This is a testable code block
117 // This is not a testable code block
120 // This is a testable code block (4-space indent)
123 You can specify that the test's execution should fail with the `should_fail`
128 // This code block is expected to generate a failure when run
132 You can specify that the code block should be compiled but not run with the
137 // This code will be compiled but not executed
141 Rustdoc also supplies some extra sugar for helping with some tedious
142 documentation examples. If a line is prefixed with `# `, then the line
143 will not show up in the HTML documentation, but it will be used when
144 testing the code block (NB. the space after the `#` is required, so
145 that one can still write things like `#[deriving(Eq)]`).
149 # /!\ The three following lines are comments, which are usually stripped off by
150 # the doc-generating tool. In order to display them anyway in this particular
151 # case, the character following the leading '#' is not a usual space like in
152 # these first five lines but a non breakable one.
153 # // showing 'fib' in this documentation would just be tedious and detracts from
154 # // what's actually being documented.
155 # fn fib(n: int) { n + 2 }
157 spawn(proc() { fib(200); })
161 The documentation online would look like `spawn(proc() { fib(200); })`, but when
162 testing this code, the `fib` function will be included (so it can compile).
164 ## Running tests (advanced)
166 Running tests often requires some special configuration to filter tests, find
167 libraries, or try running ignored examples. The testing framework that rustdoc
168 uses is build on crate `test`, which is also used when you compile crates with
169 rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness
170 with the `--test-args` flag.
173 # Only run tests containing 'foo' in their name
174 $ rustdoc --test lib.rs --test-args 'foo'
176 # See what's possible when running tests
177 $ rustdoc --test lib.rs --test-args '--help'
180 When testing a library, code examples will often show how functions are used,
181 and this code often requires `use`-ing paths from the crate. To accommodate this,
182 rustdoc will implicitly add `extern crate <crate>;` where `<crate>` is the name of
183 the crate being tested to the top of each code example. This means that rustdoc
184 must be able to find a compiled version of the library crate being tested. Extra
185 search paths may be added via the `-L` flag to `rustdoc`.
187 # Standalone Markdown files
189 As well as Rust crates, rustdoc supports rendering pure Markdown files
190 into HTML and testing the code snippets from them. A Markdown file is
191 detected by a `.md` or `.markdown` extension.
193 There are 4 options to modify the output that Rustdoc creates.
195 - `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`.
196 - `--markdown-in-header FILE`: includes the contents of `FILE` at the
197 end of the `<head>...</head>` section.
198 - `--markdown-before-content FILE`: includes the contents of `FILE`
199 directly after `<body>`, before the rendered content (including the
201 - `--markdown-after-content FILE`: includes the contents of `FILE`
202 directly before `</body>`, after all the rendered content.
204 All of these can be specified multiple times, and they are output in
205 the order in which they are specified. The first line of the file must
206 be the title, prefixed with `%` (e.g. this page has `% Rust
207 Documentation` on the first line).
209 Like with a Rust crate, the `--test` argument will run the code
210 examples to check they compile, and obeys any `--test-args` flags. The
211 tests are named after the last `#` heading.