24.11 Summary

Testing remains crucial—even in a language with strong safety guarantees like Rust. In this chapter, we covered:

  • What testing is and why it is essential for correctness.
  • Types of tests: unit tests (within the same module), integration tests (tests/ directory), and documentation tests (within doc comments).
  • Creating and running tests using #[test] and cargo test.
  • Assertion macros (assert!, assert_eq!, and assert_ne!).
  • Error handling with #[should_panic], returning Result<T, E> from tests, and handling panic messages.
  • Filtering tests by name, viewing or ignoring output, using #[ignore], and controlling concurrency.
  • Benchmarking via the nightly harness or using crates such as criterion and divan.

By combining thorough testing with Rust’s compile-time safety guarantees, you can confidently develop robust, maintainable, and high-performance systems.