Sub topic 9
Controlling examples: no_run, ignore, should_panic, compile_fail
This chapter assumes you can write /// comments with fenced examples and run cargo test --doc. Every example in a doc comment is a test by default. It is compiled and run, and it must finish without panicking. Some examples can't meet that rule, so you can add attributes to the opening fence to change what rustdoc does with them.
What each attribute does
| Attribute | Compiled? | Run? | The test passes when |
|---|---|---|---|
| (none) | Yes | Yes | It compiles and does not panic |
| `no_run` | Yes | No | It compiles |
| `ignore` | No | No | Never checked, reported as "ignored" |
| `should_panic` | Yes | Yes | It compiles and panics |
| `compile_fail` | Yes | No | Compilation fails |
You can combine attributes with commas, for example no_run,edition2021. The text attribute marks a block as plain text. Use it for output samples and shell sessions so rustdoc doesn't treat them as Rust.
no_run: check the code, don't execute it
Use no_run when running the example would have side effects or need resources the test machine lacks, such as a network, a file, or a display. The compiler still checks that the example is valid against your current API.
/// Deletes the cache directory and everything in it.
///
/// ```no_run
/// mycrate::clear_cache("/var/cache/myapp");
/// ```
pub fn clear_cache(path: &str) {
let _ = std::fs::remove_dir_all(path);
}ignore: skip the example entirely
An ignore block is not compiled, so it can drift out of date without any warning. Treat it as a last resort. It fits code that can't compile in the test environment, such as an example that needs a platform-specific library. Prefer no_run when the code merely shouldn't run.
/// Opens the default GPU device.
///
/// ```ignore
/// // Needs a GPU driver that CI machines don't have.
/// let device = mycrate::open_gpu()?;
/// ```
pub fn open_gpu() -> Result<(), std::io::Error> {
Ok(())
}Ignored tests show up in the cargo test --doc summary as ignored, so you can count them and notice when they pile up.
should_panic: document a panic with a checked example
With should_panic, the test passes only if the example panics. It pairs well with a # Panics section.
/// Returns the item at `index`.
///
/// # Panics
///
/// Panics if `index` is out of bounds.
///
/// ```should_panic
/// mycrate::nth(&[1, 2, 3], 10);
/// ```
pub fn nth(items: &[i32], index: usize) -> i32 {
items[index]
}A panic for any reason passes the test. If the example fails for an unrelated reason, such as a typo that panics earlier, the test still passes.
compile_fail: show what the compiler rejects
Use compile_fail to document code that must not compile, such as a misuse the type system prevents. The test passes only if compilation fails.
/// Takes ownership of a string.
///
/// After the call the caller can no longer use the value:
///
/// ```compile_fail
/// let s = String::from("hi");
/// mycrate::consume(s);
/// println!("{s}");
/// ```
pub fn consume(_s: String) {}Choosing between them
- Code is valid but unsafe or impossible to run in tests: use
no_run. - Code must panic to illustrate the behavior: use
should_panic. - Code must be rejected by the compiler: use
compile_fail. - Code cannot even compile in the test environment: use
ignore, and say why in a comment.
Exercise
You document a function that reads a config file from a fixed path that doesn't exist on the test machine. The example must still be checked against the current API. Which attribute should you use, and why not ignore?
Show answer
Use no_run. It compiles the example, so API changes that break it are caught, but it never runs it, so the missing file does no harm. ignore skips compilation, so the example could silently stop matching the real API.