Skip to lesson content
navan✳THE BUILD LABRUST
0/53 complete
BUILD LAB / RUST/ CONFIDENCE IN THE BUILD
BUILD NOTE 36 / 53

Test the API a caller sees.

Separate internal checks from public-contract examples.

BY NAVAN SAMI◷ 7 MIN READFOUNDATIONSRUST 2024

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.

lessons/integration-doc-tests/src/main.rs
/// 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  "));
}
↳ EXPECTED OUTPUT
build
  1. 1

    Triple-slash comments document the next item.

  2. 2

    A public library would expose the function with pub.

  3. 3

    A tests/*.rs integration test imports the package’s library name, not its private module files.

From the extracted rust-workbench folder
cargo run -p lesson_integration_doc_tests

03 Mind the boundary

04 Your next build

A SMALL MISSION

Create a documented function and a boundary test for empty input.

Reveal one possible solution +
solutions/integration-doc-tests/src/main.rs
/// 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

QUICK SIGNAL CHECK

Where do external-style Cargo integration tests usually live?

ONE MORE CONTRACT YOU CAN EXPLAIN.

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.

Built with curiosity. By Navan Sami. ↗Rust · Make the contract clear. Then build.