#[cfg(debug_assertions)] with const value to avoid warning

Hi,

I use #[cfg(debug_assertions)] to have some function available only in release mode.
To avoid warning when I am in dev mode, I wanted to put this macro just before my const HI.
But this does not effect...
How can we do the same for const to avoid warning at compile time please ?

#[cfg(debug_assertions)]
const HI : &str = "Hi";

#[cfg(debug_assertions)]
pub my_fn() {
    let hi = HI;
}

#[cfg(not(debug_assertions))]
pub my_fn() {
    println!("Here");
}

This does work, at least when written correctly. Maybe you put #! instead of #?

You could also put the const inside the function.

If I do that, it is like all the file does not exist...
In other file, the file is defined as a inexistant module...

use of undeclared crate or module `my_module`

I have another question : in test, it can't be use too like that :

#[cfg(debug_assertions)]
#[cfg(test)]
mod tests {

... tests...

Can you make an example on the playground that shows the warning you're talking about?

I don't know why you'd do this, but this also works fine.

For the first question, it works now...
But for the test nope...

The error message tells you how to fix it, although you have to type it yourself since the playground gets the location wrong if you click the suggestion in the output.

The playground also compiles the file as lib.rs when you use "Test" so it doesn't consider main to be using things. I made it pub here to fix that. This isn't an issue outside of the playground.

I made correction.
Damn, it works here...

Maybe in my real project, the only difference is that I not use #[test] on my test but #[tokio::test] ?
I will look again...

I found !
In fact I need to use cargo test --release to activate the option...

Thank you very much for your time.

1 Like