3 `rustdoc` supports executing your documentation examples as tests. This makes sure
4 that your tests are up to date and working.
6 The basic idea is this:
16 The triple backticks start and end code blocks. If this were in a file named `foo.rs`,
17 running `rustdoc --test foo.rs` will extract this example, and then run it as a test.
19 There's some subtlety though! Read on for more details.
21 ## Pre-processing examples
23 In the example above, you'll note something strange: there's no `main`
24 function! Forcing you to write `main` for every example, no matter how small,
25 adds friction. So `rustdoc` processes your examples slightly before
26 running them. Here's the full algorithm rustdoc uses to preprocess examples:
28 1. Any leading `#![foo]` attributes are left intact as crate attributes.
29 2. Some common `allow` attributes are inserted, including
30 `unused_variables`, `unused_assignments`, `unused_mut`,
31 `unused_attributes`, and `dead_code`. Small examples often trigger
33 3. If the example does not contain `extern crate`, then `extern crate
34 <mycrate>;` is inserted (note the lack of `#[macro_use]`).
35 4. Finally, if the example does not contain `fn main`, the remainder of the
36 text is wrapped in `fn main() { your_code }`.
38 For more about that caveat in rule 3, see "Documeting Macros" below.
40 ## Hiding portions of the example
42 Sometimes, you need some setup code, or other things that would distract
43 from your example, but are important to make the tests work. Consider
44 an example block that looks like this:
47 /// Some documentation.
51 It will render like this:
54 /// Some documentation.
58 Yes, that's right: you can add lines that start with `# `, and they will
59 be hidden from the output, but will be used when compiling your code. You
60 can use this to your advantage. In this case, documentation comments need
61 to apply to some kind of function, so if I want to show you just a
62 documentation comment, I need to add a little function definition below
63 it. At the same time, it's only there to satisfy the compiler, so hiding
64 it makes the example more clear. You can use this technique to explain
65 longer examples in detail, while still preserving the testability of your
68 For example, imagine that we wanted to document this code:
73 println!("{}", x + y);
76 We might want the documentation to end up looking like this:
78 > First, we set `x` to five:
83 > # println!("{}", x + y);
86 > Next, we set `y` to six:
91 > # println!("{}", x + y);
94 > Finally, we print the sum of `x` and `y`:
99 > println!("{}", x + y);
102 To keep each code block testable, we want the whole program in each block, but
103 we don't want the reader to see every line every time. Here's what we put in
107 First, we set `x` to five:
112 # println!("{}", x + y);
115 Next, we set `y` to six:
120 # println!("{}", x + y);
123 Finally, we print the sum of `x` and `y`:
128 println!("{}", x + y);
132 By repeating all parts of the example, you can ensure that your example still
133 compiles, while only showing the parts that are relevant to that part of your
136 Another case where the use of `#` is handy is when you want to ignore
137 error handling. Lets say you want the following,
141 /// let mut input = String::new();
142 /// io::stdin().read_line(&mut input)?;
145 The problem is that `?` returns a `Result<T, E>` and test functions
146 don't return anything so this will give a mismatched types error.
149 /// A doc test using ?
153 /// # fn foo() -> io::Result<()> {
154 /// let mut input = String::new();
155 /// io::stdin().read_line(&mut input)?;
162 You can get around this by wrapping the code in a function. This catches
163 and swallows the `Result<T, E>` when running tests on the docs. This
164 pattern appears regularly in the standard library.
166 ### Documenting macros
168 Here’s an example of documenting a macro:
171 /// Panic with a given message unless an expression evaluates to true.
176 /// # #[macro_use] extern crate foo;
178 /// panic_unless!(1 + 1 == 2, “Math is broken.”);
182 /// ```rust,should_panic
183 /// # #[macro_use] extern crate foo;
185 /// panic_unless!(true == false, “I’m broken.”);
189 macro_rules! panic_unless {
190 ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
195 You’ll note three things: we need to add our own `extern crate` line, so that
196 we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
197 `main()` as well (for reasons discussed above). Finally, a judicious use of
198 `#` to comment out those two things, so they don’t show up in the output.
202 There are a few annotations that are useful to help `rustdoc` do the right
203 thing when testing your code:
212 The `ignore` directive tells Rust to ignore your code. This is almost never
213 what you want, as it's the most generic. Instead, consider annotating it
214 with `text` if it's not code, or using `#`s to get a working example that
215 only shows the part you care about.
224 `should_panic` tells `rustdoc` that the code should compile correctly, but
225 not actually pass as a test.
230 /// println!("Hello, world");
236 The `no_run` attribute will compile your code, but not run it. This is
237 important for examples such as "Here's how to retrieve a web page,"
238 which you would want to ensure compiles, but might be run in a test
239 environment that has no network access.