# How do you setup rust code coverage?

**URL:** <https://discourse.flox.dev/t/how-do-you-setup-rust-code-coverage/1108>\
**Category:** Help\
**Created:** [March 27, 2025, 10:39pm UTC](https://discourse.flox.dev/t/how-do-you-setup-rust-code-coverage/1108 "2025-03-27T22:39:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![five-vee](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@five-vee](https://discourse.flox.dev/u/five-vee)\
**Post date:** [March 27, 2025, 10:39pm UTC](https://discourse.flox.dev/t/how-do-you-setup-rust-code-coverage/1108/1 "2025-03-27T22:39:38Z")

</div>

So I’m following Flox’s [Rust Cookbook](https://flox.dev/docs/cookbook/languages/rust/), and I was surprised to learn that it doesn’t support code coverage. Or, at least, not in the way I expected.

Apparently, for [`-C instrument-coverage`](https://doc.rust-lang.org/beta/rustc/instrument-coverage.html) to work, I’ll need to have built `rustc` with `profiler = true` in the compiler’s TOML file.

Flox provides Rust packages, but I’m not sure how, if possible, to get a `rustc` (and thus `cargo`) that can do code coverage.

---

<div class="post-metadata">

**Author:** ![five-vee](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@five-vee](https://discourse.flox.dev/u/five-vee)\
**Post date:** [March 30, 2025, 8:41pm UTC](https://discourse.flox.dev/t/how-do-you-setup-rust-code-coverage/1108/2 "2025-03-30T20:41:15Z")

</div>

I gave up and just went back to `rustup.`

---

<div class="post-metadata">

**Author:** ![mkenigs](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.flox.dev/mkenigs/32/520_2.png) [@mkenigs](https://discourse.flox.dev/u/mkenigs)\
**Post date:** [April 2, 2025, 4:57pm UTC](https://discourse.flox.dev/t/how-do-you-setup-rust-code-coverage/1108/3 "2025-04-02T16:57:38Z")

</div>

In case it’s still any help:

Rust nightly includes support for code coverage, so you can follow the cookbook’s instructions for using a nightly compiler: [Rust - Flox Docs](https://flox.dev/docs/cookbook/languages/rust/#how-do-i-use-nightly-compilers)

You’d probably want to create your own flake with a nightly compiler rather than using `github:zmitchell/rust-toolchains#nightly`, but using a nightly seems to work where using plain cargo fails:

```auto
flox [coverage] /tmp/coverage main > flox install cargo
✅ 'cargo' installed to environment 'coverage'
flox [coverage] /tmp/coverage main > RUSTFLAGS="-C instrument-coverage" cargo build
   Compiling coverage v0.1.0 (/private/tmp/coverage)
error[E0463]: can't find crate for `profiler_builtins`
  |
  = note: the compiler may have been built without the profiler runtime

For more information about this error, try `rustc --explain E0463`.
error: could not compile `coverage` (bin "coverage") due to 1 previous error

flox [coverage] /tmp/coverage main > flox remove cargo
🗑️ 'cargo' uninstalled from environment 'coverage'

flox [coverage] /tmp/coverage main > flox install github:zmitchell/rust-toolchains#nightly
✅ 'nightly' installed to environment 'coverage'

flox [coverage] /tmp/coverage main > RUSTFLAGS="-C instrument-coverage" cargo build
   Compiling coverage v0.1.0 (/private/tmp/coverage)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.01s

flox [coverage] /tmp/coverage main > ./target/debug/coverage
Hello, world!

flox [coverage] /tmp/coverage main > ls | grep profraw
default_12879082849180275691_0_74579.profraw

```
