0Pricing
Learn Rust Coding · Lesson

Doc Tests

Tested examples.

What Are Doc Tests?

Doc tests are code examples inside doc comments that Cargo actually compiles and runs. They keep your documentation correct and your examples working.

Code in Doc Comments

Put example code in a fenced code block (three backticks) inside a /// comment, under an # Examples heading. The default language is Rust.

/// Doubles a number.
///
/// # Examples
///
///     let result = my_crate::double(4);
///     assert_eq!(result, 8);
pub fn double(n: i32) -> i32 {
    n * 2
}

All lessons in this course

  1. Unit Tests
  2. Integration Tests
  3. Doc Comments
  4. Doc Tests
← Back to Learn Rust Coding