4

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?

3
  • Why can't you just omit the cfg directive?
    – isaactfa
    Commented May 7, 2022 at 13:22
  • 2
    any(foo, not(foo)) is always true. #[cfg(any(foo, not(foo)))] will always evaluate Commented May 7, 2022 at 14:25
  • @isaactfa for testing purpose. For example in test of a macro.
    – Kos
    Commented May 7, 2022 at 14:50

1 Answer 1

12

Just do this for a cfg option that is always true:

#[cfg(all())]
fn main() {
    println!("It works!");
}

And if you need a cfg option that is always false you can use:

#[cfg(any())]
fn main() {
    println!("It disappears!");
}
2
  • 1
    I used to write #[cfg(xxx)] to comment out some block of code, but this looks nicer, and can be changed from on to off just by overwriting two characters.
    – rodrigo
    Commented May 7, 2022 at 18:06
  • Amazing! That's what I had been looking for.
    – Kos
    Commented May 8, 2022 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.