Sub topic 10
How rustdoc compiles and runs doc tests
This chapter assumes you can write /// comments with fenced examples and run cargo test --doc. Here you will learn what happens between that command and the pass or fail result. Knowing this explains most surprising doc test failures.
One example, one program
rustdoc does not run your examples inside your library. It extracts every Rust code block from the doc comments, and it compiles each block as its own small program. Each program is then run on its own.
The steps for each block are:
- Collect the block and any hidden
#lines. - Wrap the code in
fn main() { ... }, unless the block already definesfn main. - Compile the program as a separate crate that depends on your library.
- Run the executable. Exit code 0 means pass. A panic or any other nonzero exit means fail.
What the wrapped program looks like
Suppose an example calls mycrate::double(4) and asserts the result. The generated program is roughly this:
extern crate mycrate;
fn main() {
let n = mycrate::double(4);
assert_eq!(n, 8);
}rustdoc adds the extern crate line for you, so the example can refer to mycrate directly. The program uses the same edition as your crate, as set in Cargo.toml.
Only the public API is visible
The example crate is a separate crate. It links to your library the way any other user would, so it can reach only pub items. An example that calls a private function, or a pub(crate) one, fails to compile. This is intended. Your examples prove that the documented public interface works for outside users.
If you want to show a private helper in a comment, mark the block ignore or leave it as plain text. Its code is not built as part of the library.
Consequences of the model
| Behavior | Why it happens |
|---|---|
| Examples cannot share variables or imports | Each block is a separate program. |
| Every example needs its own `use` lines | Imports from one block do not carry to another. |
| Many doc tests make `cargo test --doc` slow | Each one is a full compile and link. |
| Doc tests do not run for binary-only crates | There is no library to link against. |
| Failure output names a file and line | The name points to the code block in your source. |
Wrapping and `fn main`
Automatic wrapping has one catch. If your block contains fn main, rustdoc leaves it as written. This is useful when you need items such as structs or use statements outside main. It also means that a fn main you add by accident can change how ? behaves (see the next chapter).
rustdoc also places the block's #![...] attributes at crate level before wrapping. Without that, they would end up inside main.
Checking test names
To see which examples exist, list them without running them:
cargo test --doc -- --listEach name looks like src/lib.rs - add (line 5). You can filter by a part of that name:
cargo test --doc addCommon mistakes
- Forgetting
use mycrate::item;in an example. The compiler reports an unresolved name. - Documenting a private function with a runnable example. The test fails because the item is not reachable.
- Writing a long example that sets up a lot of state. Each run pays the full compile cost, so use hidden lines or
no_runinstead.
Exercise
A doc example for a pub(crate) function calls it as mycrate::helper(), and cargo test --doc reports a compile error. Why does it fail, and what are two fixes?
Show answer
Each doc test is compiled as a separate crate that links to the library, so it sees only the public API, and pub(crate) items are not visible to it. One fix is to make the function pub if it belongs in the public API. The other is to mark the example ignore or turn it into plain text, so it is not compiled as a test.