Here’s the translation of the Go testing and benchmarking example to Rust:
Unit testing is an important part of writing principled Rust programs. The #[test] attribute and the assert! macro provide the tools we need to write unit tests, and the cargo test command runs tests.
For the sake of demonstration, this code is in the main module, but it could be any module. Testing code typically lives in the same module as the code it tests.
Run all tests in the current project in verbose mode:
Run all benchmarks in the current project. Note that benchmarks are unstable in Rust and require the nightly toolchain:
In Rust, benchmarking is not as integrated into the standard tooling as it is in Go. For more sophisticated benchmarking, consider using external crates like criterion or bencher.