Test the API a caller sees.
Separate internal checks from public-contract examples.
01 Understand the contract
Integration tests in a package’s tests directory use the library as an external caller would. This reveals whether your public API is sufficient without relying on private details. Rustdoc extracts executable examples from documentation comments and runs them as documentation tests when Cargo tests the library. Documentation should explain behavior, errors, and important constraints. Keep runnable examples small enough that a reader can see the contract; use the cookbook’s reusable-library project to explore the full file layout.
02 Make it concrete
This is a complete standard-library example. Use Rust stable with the 2024 edition. Run it locally using the Rust workbench.
/// Return a trimmed, lowercase key for simple ASCII-style labels.
/// Unicode lowercase is supported, but this is not full case-folding.
fn key(value: &str) -> String {
value.trim().to_lowercase()
}
fn main() {
println!("{}", key(" BUILD "));
}build
- 1
Triple-slash comments document the next item.
- 2
A public library would expose the function with pub.
- 3
A tests/*.rs integration test imports the package’s library name, not its private module files.
cargo run -p lesson_integration_doc_tests03 Mind the boundary
04 Your next build
Create a documented function and a boundary test for empty input.
Reveal one possible solution +
/// Count whitespace-separated words. Empty input has zero words.
fn words(text: &str) -> usize {
text.split_whitespace().count()
}
#[test]
fn empty() {
assert_eq!(words(" "), 0);
}
fn main() {
assert_eq!(words("one two"), 2);
}05 Check your reasoning
Where do external-style Cargo integration tests usually live?
Understand it.
Then make it yours.
Run the example, try your own input, and explain the boundary. Log the lesson when the idea makes sense.