Cfg annotation for debugging

⚓ rust    📅 2025-05-20    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 41 days ago. The information described in this article may have changed.

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.

#[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?

3 posts - 3 participants

Read full topic

🏷️ rust_feed