Rust Documentation Documentation Tests


Code in documentation comments will automatically be executed by cargo test. These are known as "documentation tests", and help to ensure that your examples work and will not mislead users of your crate.

You can import relative from the crate root (as if there were a hidden extern crate mycrate; at the top of the example)

/// ```
/// use mycrate::foo::Bar;
/// ```

If your code may not execute properly in a documentation test, you can use the no_run attribute, like so:

/// ```no_run
/// use mycrate::NetworkClient;
/// NetworkClient::login("foo", "bar");
/// ```

You can also indicate that your code should panic, like this:

/// ```should_panic
/// unreachable!();
/// ```