On this page
Code Coverage in Rust: -Z profile vs -C instrument-coverage
An overview of collecting code coverage in Rust using GCC-compatible gcov and LLVM native instrumentation.
Collecting code coverage data in Rust is essential for understanding how well tests exercise your codebase and where uncovered execution paths exist.
Rust provides two primary mechanisms to collect code coverage data:
- Using
-Z profile - Using
-C instrument-coverage
1. Using -Z profile
This method leverages GCC-compatible gcov-based coverage implementation. In simple terms, it gathers coverage data based on DebugInfo.
RUSTFLAGS="-Z profile" cargo test
This is very handy when you want to see which branches and statements are being executed during tests. It provides a straightforward execution roadmap across your code.
2. Using -C instrument-coverage
This flag enables LLVM’s native source-based coverage instrumentation, known for its efficiency and high precision:
RUSTFLAGS="-C instrument-coverage" cargo test
LLVM native coverage provides accurate, line-by-line and region-by-region coverage data. It integrates cleanly with tools like llvm-cov and cargo-llvm-cov.
When looking to improve the robustness and reliability of your test suites, -C instrument-coverage is generally the recommended approach for modern Rust development.