Testing and Documentation
Write unit tests, integration tests, and generate documentation for your Rust projects.
Rust Documentation Generation with cargo doc
Introduction to Rust's Documentation Generation System
Rust has a fantastic built-in documentation generation system driven by comments within your code.
This allows you to create beautiful and informative API documentation directly from your source.
The primary tool for this is cargo doc
, a subcommand of Rust's package manager, Cargo.
It parses specially formatted comments and transforms them into a static website, making your code
easy to understand and use.
Rust uses a specific style of comments called "doc comments" to extract information for documentation. These comments start with ///
for single-line comments and /** ... */
for multi-line comments. These comments should be placed immediately before the item they are documenting (e.g., a function, struct, module, etc.).
Using cargo doc
to Create API Documentation
The cargo doc
command simplifies the process of creating and viewing documentation for your Rust crates.
Here's a breakdown of how to use it:
Basic Usage
To generate documentation for your crate, navigate to the root directory of your project (where your Cargo.toml
file is located) and run the following command:
cargo doc
This command will compile your crate and extract all the doc comments, generating HTML documentation in the target/doc
directory.
Opening the Documentation
To automatically open the generated documentation in your web browser, use the --open
flag:
cargo doc --open
Cargo will generate the documentation and then open it in your default browser.
Documenting Your Code
Let's see how to write doc comments in your code:
/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// let result = my_crate::add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
/// A struct representing a Point in 2D space.
#[derive(Debug)]
pub struct Point {
/// The x coordinate of the point.
pub x: i32,
/// The y coordinate of the point.
pub y: i32,
}
In the example above:
- The
///
comments provide explanations for theadd
function and thePoint
struct. - The
# Examples
section demonstrates how to use theadd
function, including a runnable code block within triple backticks. This example is tested when the documentation is built, ensuring your examples remain valid as your code changes! - Field comments (e.g.,
/// The x coordinate of the point.
) document the members of thePoint
struct.
Features
Cargo doc supports documenting features of your crate. If you have feature-specific code, you can document it like this:
#[cfg(feature = "my_feature")]
/// This function is only available when the `my_feature` feature is enabled.
pub fn my_feature_function() {
println!("My Feature is enabled!");
}
Cargo doc will only include this in the generated documentation when the my_feature
feature is specified.
You can generate documentation for specific features with cargo doc --features my_feature
Best Practices
- **Be concise and clear:** Write documentation that is easy to understand and directly addresses the purpose of the item being documented.
- **Use examples:** Include code examples to demonstrate how to use your functions, structs, and modules. Runnable examples are best as they are tested and kept up to date.
- **Explain parameters and return values:** Clearly describe the purpose of each parameter and the meaning of the return value.
- **Document errors:** If a function can return an error, explain what circumstances cause the error.
- **Keep your documentation up-to-date:** Review and update your documentation whenever you make changes to your code.