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 // the "link" crate attribute is currently required for rustdoc, but normally
16 #![crate_id = "universe"]
19 //! Tools for dealing with universes (this is a doc comment, and is shown on
20 //! the crate index page. The ! makes it apply to the parent of the comment,
21 //! rather than what follows).
23 # mod workaround_the_outer_function_rustdoc_inserts {
24 /// Widgets are very common (this is a doc comment, and will show up on
25 /// Widget's documentation).
27 /// All widgets have a purpose (this is a doc comment, and will show up
28 /// the field's documentation).
30 /// Humans are not allowed to understand some widgets
34 pub fn recalibrate() {
35 //! Recalibrate a pesky universe (this is also a doc comment, like above,
36 //! the documentation will be applied to the *parent* item, so
43 Doc comments are markdown, and are currently parsed with the
44 [sundown][sundown] library. rustdoc does not yet do any fanciness such as
45 referencing other items inline, like javadoc's `@see`. One exception to this
46 is that the first paragraph will be used as the "summary" of an item in the
47 generated documentation:
50 /// A whizbang. Does stuff. (this line is the summary)
56 To generate the docs, run `rustdoc universe.rs`. By default, it generates a
57 directory called `doc`, with the documentation for `universe` being in
58 `doc/universe/index.html`. If you are using other crates with `extern crate`,
59 rustdoc will even link to them when you use their types, as long as their
60 documentation has already been generated by a previous run of rustdoc, or the
61 crate advertises that its documentation is hosted at a given URL.
63 The generated output can be controlled with the `doc` crate attribute, which
64 is how the above advertisement works. An example from the `libstd`
68 #[doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
69 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
70 html_root_url = "http://static.rust-lang.org/doc/master")];
73 The `html_root_url` is the prefix that rustdoc will apply to any references to
74 that crate's types etc.
76 rustdoc can also generate JSON, for consumption by other tools, with
77 `rustdoc --output-format json`, and also consume already-generated JSON with
78 `rustdoc --input-format json`.
80 # Using the Documentation
82 The web pages generated by rustdoc present the same logical hierarchy that one
83 writes a library with. Every kind of item (function, struct, etc) has its own
84 color, and one can always click on a colored type to jump to its
85 documentation. There is a search bar at the top, which is powered by some
86 JavaScript and a statically-generated search index. No special web server is
87 required for the search.
89 [sundown]: https://github.com/vmg/sundown/
91 # Testing the Documentation
93 `rustdoc` has support for testing code examples which appear in the
94 documentation. This is helpful for keeping code examples up to date with the
97 To test documentation, the `--test` argument is passed to rustdoc:
100 rustdoc --test crate.rs
105 Rust documentation currently uses the markdown format, and rustdoc treats all
106 code blocks as testable-by-default. In order to not run a test over a block of
107 code, the `ignore` string can be added to the three-backtick form of markdown
112 // This is a testable code block
116 // This is not a testable code block
119 // This is a testable code block (4-space indent)
122 You can specify that the test's execution should fail with the `should_fail`
127 // This code block is expected to generate a failure when run
131 You can specify that the code block should be compiled but not run with the
136 // This code will be compiled but not executed
140 Rustdoc also supplies some extra sugar for helping with some tedious
141 documentation examples. If a line is prefixed with `# `, then the line
142 will not show up in the HTML documentation, but it will be used when
143 testing the code block (NB. the space after the `#` is required, so
144 that one can still write things like `#[deriving(Eq)]`).
148 # /!\ The three following lines are comments, which are usually stripped off by
149 # the doc-generating tool. In order to display them anyway in this particular
150 # case, the character following the leading '#' is not a usual space like in
151 # these first five lines but a non breakable one.
152 # // showing 'fib' in this documentation would just be tedious and detracts from
153 # // what's actually being documented.
154 # fn fib(n: int) { n + 2 }
156 spawn(proc() { fib(200); })
160 The documentation online would look like `spawn(proc() { fib(200); })`, but when
161 testing this code, the `fib` function will be included (so it can compile).
163 ## Running tests (advanced)
165 Running tests often requires some special configuration to filter tests, find
166 libraries, or try running ignored examples. The testing framework that rustdoc
167 uses is build on crate `test`, which is also used when you compile crates with
168 rustc's `--test` flag. Extra arguments can be passed to rustdoc's test harness
169 with the `--test-args` flag.
172 # Only run tests containing 'foo' in their name
173 $ rustdoc --test lib.rs --test-args 'foo'
175 # See what's possible when running tests
176 $ rustdoc --test lib.rs --test-args '--help'
179 When testing a library, code examples will often show how functions are used,
180 and this code often requires `use`-ing paths from the crate. To accommodate this,
181 rustdoc will implicitly add `extern crate <crate>;` where `<crate>` is the name of
182 the crate being tested to the top of each code example. This means that rustdoc
183 must be able to find a compiled version of the library crate being tested. Extra
184 search paths may be added via the `-L` flag to `rustdoc`.
186 # Standalone Markdown files
188 As well as Rust crates, rustdoc supports rendering pure Markdown files
189 into HTML and testing the code snippets from them. A Markdown file is
190 detected by a `.md` or `.markdown` extension.
192 There are 4 options to modify the output that Rustdoc creates.
194 - `--markdown-css PATH`: adds a `<link rel="stylesheet">` tag pointing to `PATH`.
195 - `--markdown-in-header FILE`: includes the contents of `FILE` at the
196 end of the `<head>...</head>` section.
197 - `--markdown-before-content FILE`: includes the contents of `FILE`
198 directly after `<body>`, before the rendered content (including the
200 - `--markdown-after-content FILE`: includes the contents of `FILE`
201 directly before `</body>`, after all the rendered content.
203 All of these can be specified multiple times, and they are output in
204 the order in which they are specified. The first line of the file must
205 be the title, prefixed with `%` (e.g. this page has `% Rust
206 Documentation` on the first line).
208 Like with a Rust crate, the `--test` argument will run the code
209 examples to check they compile, and obeys any `--test-args` flags. The
210 tests are named after the last `#` heading.