# Cfg annotation for debugging

**URL:** <https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772>\
**Category:** help\
**Created:** [May 20, 2025, 11:43am UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772 "2025-05-20T11:43:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GunnarBernsteinHH](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gunnarbernsteinhh/32/38684_2.png) [@GunnarBernsteinHH](https://users.rust-lang.org/u/GunnarBernsteinHH)\
**Post date:** [May 20, 2025, 11:43am UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/1 "2025-05-20T11:43:38Z")

</div>

I have some very detailed outputs for my code, which I want to run only during debugging.

I started with `#cfg(debug_assertions)` but this also runs when I do `cargo run`.

So I created a feature "my\_debug" which I added only in the debug configuration and changed the code accordingly: `#[cfg(all(debug_assertions, feature = "my_debug"))]`.

This actually works fine when running and debugging the code, but has the drawback, that the code is grayed out. This has a number of drawbacks, e.g. left out during renaming.

So I added `my_debug` to the settings in `rust-analyzer.cargo.features`.  
Now the code is not grayed out anymore, but now I get errors.

Only in my output I use the crate `utils.rs`.

```rust
#[cfg(all(debug_assertions, feature = "debug"))]
use crate::utils::U64Ext;

```

This compiles and runs fine, BUT the analyzer now marks the use statement as 'unused' and gives me a warning. When I later use that crate it shows me an error, because it is not imported.

Furthermore, now the feature my\_debug is also used during `test`, which I do not want.

I can remove the cfg-directive, but then I get warnings during actual compilation.

So as a last resort I need to qualify the crate on each statement, which I find a bit cumbersome.

Any idea how I can resolve this?

SOLVED:  
To "ungray" the code, I just needed to add `default = ["my_debug"]` in the cargo.toml. Then it will also be included in renaming for example. I did try this, but sometimes this takes a while before taking effect.

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [May 20, 2025, 11:58am UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/2 "2025-05-20T11:58:54Z")

</div>

> [@GunnarBernsteinHH](#):
>
> I have some very detailed outputs for my code, which I want to run only during debugging.

maybe you need a proper logging facility with configurable filtering functionality, such as `log` and `tracing`.

> [@GunnarBernsteinHH](#):
>
> I started with `#cfg(debug_assertions)` but this also runs when I do `cargo run`.

you can use `cargo run --release`

> [@GunnarBernsteinHH](#):
>
> So as a last resort I need to qualify the crate on each statement, which I find a bit cumbersome.

you can use the `cfg-if` crate to reduce some clutter.

---

<div class="post-metadata">

**Author:** ![00100011](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/00100011/32/47257_2.png) [@00100011](https://users.rust-lang.org/u/00100011)\
**Post date:** [May 20, 2025, 12:09pm UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/3 "2025-05-20T12:09:05Z")

</div>

> [@GunnarBernsteinHH](#):
>
> I have some very detailed outputs for my code, which I want to run only during debugging.

Alternative spelling: you're logging out different parts of your program with different levels of verbosity, which you'd like to be able to configure on a bit more granular of a level than the one provided by a fairly black-and-white `#[cfg(debug_assertions)]` annotation and/or a feature flag.

It's a fairly common problem, with a set of fairly common solutions. You could spend a bunch of time reinventing your own proverbial wheel, or just use a crate like [`log`](https://crates.io/crates/log) or [`tracing`](https://crates.io/crates/tracing) instead.

---

<div class="post-metadata">

**Author:** ![GunnarBernsteinHH](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/gunnarbernsteinhh/32/38684_2.png) [@GunnarBernsteinHH](https://users.rust-lang.org/u/GunnarBernsteinHH)\
**Post date:** [May 22, 2025, 6:03pm UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/4 "2025-05-22T18:03:27Z")

</div>

Thanks for the hint on the log crates. That can simplify the output indeed.

But I am not sure if that solves the issue with my conditional structs, which contain fields that I do not want in the release build.

```rust

This is a struct in my program:
/// Single Step when run, records the state before to identify loops
#[derive(Clone)]
struct StepExpanding {
    for_symbol_state: i16,
    tape_before: TapeType,
    high_bound_before: BitType,
    low_bound_before: BitType,

    #[cfg(all(debug_assertions, feature = "my_debug"))]
    pub text: [char; 3],
}

```

This is not included in the release build, but should be available during debugging. I do not mind if it is included when using cargo run actually, the problem is, that tests fail for verbose output.

---

<div class="post-metadata">

**Author:** ![00100011](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/00100011/32/47257_2.png) [@00100011](https://users.rust-lang.org/u/00100011)\
**Post date:** [May 22, 2025, 7:06pm UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/5 "2025-05-22T19:06:39Z")

</div>

This particular bit:

> [@GunnarBernsteinHH](#):
>
> But I am not sure if that solves the issue with my conditional structs, which contain fields that I do not want in the release build.

and this one:

> [@GunnarBernsteinHH](#):
>
> the problem is, that tests fail for verbose output.

aren't exactly the same issues.

> [@GunnarBernsteinHH](#):
>
> This is not included in the release build, but should be available during debugging.

I'd consider extracting them into some static variables instead, using [`static_init`](https://crates.io/crates/static_init) or the like.

In the actual [code](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=d9019ce2c0b0961f55f2c6fa322a374b) then, "macro gate" the particularly verbose parts:

```rust
use log::{error, warn, info, debug, trace};
use env_logger::{Builder as Logger, Env}; // 0.11.8

fn setup() {
    let env = Env::default()
        // change to "trace" to "populate fields"
        .default_filter_or("debug"); 
    Logger::from_env(env).init();
}

fn main() {
    setup();
    // use any/all of these
    error!("error here");
    warn!("warn here");
    info!("info here");
    debug!("debug here");
    // for complex operations:
    trace!("trace here: {}", {
        println!("<populating struct fields>");
        std::thread::sleep(std::time::Duration::from_secs(5));
        println!("<struct fields populated>");
        "done"
    });
}

```

---

<div class="post-metadata">

**Author:** ![Redglyph](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/redglyph/32/27961_2.png) [@Redglyph](https://users.rust-lang.org/u/Redglyph)\
**Post date:** [May 22, 2025, 8:31pm UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/6 "2025-05-22T20:31:20Z")

</div>

> [@GunnarBernsteinHH](#):
>
> So I created a feature "my\_debug" which I added only in the debug configuration and changed the code accordingly: `#[cfg(all(debug_assertions, feature = "my_debug"))]`.
> 
> This actually works fine when running and debugging the code, but has the drawback, that the code is grayed out. This has a number of drawbacks, e.g. left out during renaming.

That's strange, and not a little worrying! You should probably see if that's a bug or change. If you switch to RustRover, you'll see that it keeps performing the refactoring operations on disabled code as expected (for ex. if a `#[cfg(any())]` temporarily disables a block or if a feature is not enabled), and it includes those parts when you search where some item is used. You can also choose to keep the syntax highlighting but with a different background or some effect like borders or strikethrough.

For debugging, I'm using a very basic technique: I define `const VERBOSE` bools that I change manually when I need to display more information, which often implies additional instructions than just the `println!`. All those debug parts are simply behind `if VERBOSE {...}`s. I usually use one constant per function, implementation, or module during the development, and if I want a more fine-grained control, I can copy the declaration more locally to override the outer one, like inside a particular block. I've never felt the need for anything else so far. It could be a complement to the config system you're using.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [August 20, 2025, 8:31pm UTC](https://users.rust-lang.org/t/cfg-annotation-for-debugging/129772/7 "2025-08-20T20:31:46Z")

</div>

Closed on August 20, 2025, 8:31pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
