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 Please note that by default, if no language is set for the block code, `rustdoc`
20 assumes it is `Rust` code. So the following:
26 is strictly equivalent to:
32 There's some subtlety though! Read on for more details.
34 ## Pre-processing examples
36 In the example above, you'll note something strange: there's no `main`
37 function! Forcing you to write `main` for every example, no matter how small,
38 adds friction. So `rustdoc` processes your examples slightly before
39 running them. Here's the full algorithm rustdoc uses to preprocess examples:
41 1. Any leading `#![foo]` attributes are left intact as crate attributes.
42 2. Some common `allow` attributes are inserted, including
43 `unused_variables`, `unused_assignments`, `unused_mut`,
44 `unused_attributes`, and `dead_code`. Small examples often trigger
46 3. If the example does not contain `extern crate`, then `extern crate
47 <mycrate>;` is inserted (note the lack of `#[macro_use]`).
48 4. Finally, if the example does not contain `fn main`, the remainder of the
49 text is wrapped in `fn main() { your_code }`.
51 For more about that caveat in rule 3, see "Documeting Macros" below.
53 ## Hiding portions of the example
55 Sometimes, you need some setup code, or other things that would distract
56 from your example, but are important to make the tests work. Consider
57 an example block that looks like this:
60 /// Some documentation.
64 It will render like this:
67 /// Some documentation.
71 Yes, that's right: you can add lines that start with `# `, and they will
72 be hidden from the output, but will be used when compiling your code. You
73 can use this to your advantage. In this case, documentation comments need
74 to apply to some kind of function, so if I want to show you just a
75 documentation comment, I need to add a little function definition below
76 it. At the same time, it's only there to satisfy the compiler, so hiding
77 it makes the example more clear. You can use this technique to explain
78 longer examples in detail, while still preserving the testability of your
81 For example, imagine that we wanted to document this code:
86 println!("{}", x + y);
89 We might want the documentation to end up looking like this:
91 > First, we set `x` to five:
96 > # println!("{}", x + y);
99 > Next, we set `y` to six:
104 > # println!("{}", x + y);
107 > Finally, we print the sum of `x` and `y`:
112 > println!("{}", x + y);
115 To keep each code block testable, we want the whole program in each block, but
116 we don't want the reader to see every line every time. Here's what we put in
120 First, we set `x` to five:
125 # println!("{}", x + y);
128 Next, we set `y` to six:
133 # println!("{}", x + y);
136 Finally, we print the sum of `x` and `y`:
141 println!("{}", x + y);
145 By repeating all parts of the example, you can ensure that your example still
146 compiles, while only showing the parts that are relevant to that part of your
149 Another case where the use of `#` is handy is when you want to ignore
150 error handling. Lets say you want the following,
154 /// let mut input = String::new();
155 /// io::stdin().read_line(&mut input)?;
158 The problem is that `?` returns a `Result<T, E>` and test functions
159 don't return anything so this will give a mismatched types error.
162 /// A doc test using ?
166 /// # fn foo() -> io::Result<()> {
167 /// let mut input = String::new();
168 /// io::stdin().read_line(&mut input)?;
175 You can get around this by wrapping the code in a function. This catches
176 and swallows the `Result<T, E>` when running tests on the docs. This
177 pattern appears regularly in the standard library.
179 ### Documenting macros
181 Here’s an example of documenting a macro:
184 /// Panic with a given message unless an expression evaluates to true.
189 /// # #[macro_use] extern crate foo;
191 /// panic_unless!(1 + 1 == 2, “Math is broken.”);
196 /// # #[macro_use] extern crate foo;
198 /// panic_unless!(true == false, “I’m broken.”);
202 macro_rules! panic_unless {
203 ($condition:expr, $($rest:expr),+) => ({ if ! $condition { panic!($($rest),+); } });
208 You’ll note three things: we need to add our own `extern crate` line, so that
209 we can add the `#[macro_use]` attribute. Second, we’ll need to add our own
210 `main()` as well (for reasons discussed above). Finally, a judicious use of
211 `#` to comment out those two things, so they don’t show up in the output.
215 There are a few annotations that are useful to help `rustdoc` do the right
216 thing when testing your code:
225 The `ignore` directive tells Rust to ignore your code. This is almost never
226 what you want, as it's the most generic. Instead, consider annotating it
227 with `text` if it's not code, or using `#`s to get a working example that
228 only shows the part you care about.
237 `should_panic` tells `rustdoc` that the code should compile correctly, but
238 not actually pass as a test.
243 /// println!("Hello, world");
249 `compile_fail` tells `rustdoc` that the compilation should fail. If it
250 compiles, then the test will fail. However please note that code failing
251 with the current Rust release may work in a future release, as new features
257 /// x += 2; // shouldn't compile!
261 The `no_run` attribute will compile your code, but not run it. This is
262 important for examples such as "Here's how to retrieve a web page,"
263 which you would want to ensure compiles, but might be run in a test
264 environment that has no network access.