`cfg` which is always true / false?

For testing purpose I need cfg which is always true / false.
For true I use

#[ cfg( target_pointer_width = "64") ]
...

But obviously it is not general enough.
What is optimal way of expressing cfg to get necessary value?

This is just a hack, but you could use something like this:

#[cfg(feature = "alwaysfalse")]
println!("this will never be printed");
#[cfg(not(feature = "alwaysfalse"))]
println!("this will always be printed");

This will break down if you add the alwaysfalse feature to your Cargo.toml, and then compile with cargo build --features alwaysfalse... But that's under your control.

2 Likes

Cool! That should work. Thank you!

fn main() {
    #[cfg(any())]
    println!("Never");
    #[cfg(all())]
    println!("Always");
}

(Playground)

Output:

Always

13 Likes

Amazing! It is even better! Thanks!