Chapter 24: Testing in Rust
Software testing is essential for verifying code correctness, particularly when refactoring or adding features. Rust’s strong compile-time safety checks eliminate entire classes of bugs prevalent in C and C++, such as use-after-free, null pointer dereferencing, and many buffer overflows. However, these checks primarily ensure memory and type safety, not the correctness of the application’s logic or its adherence to requirements. Therefore, testing remains crucial in Rust for validating behavior, logic, and performance.
This chapter introduces Rust’s integrated testing framework and common practices. We will cover unit, integration, and documentation tests, techniques for running tests selectively, handling expected failures, using test-specific dependencies, and briefly introduce benchmarking. Comparisons to C/C++ testing practices will be made where relevant.