#ifdef FALSE for Rust?

I've got a question about how I can do something like C's

#ifdef False
// code
#endif

In my workflow I sometimes like to 'disable' code from compilation, as I may not need it soon (or possibly ever).

I am currently using `#[cfg(windows)]' because I never use the Windows platform.

So, it's kind of like #ifdef FALSE in C.

I could comment the code out, but I prefer using #[cfg(xxx)] because the code will get some syntax analysis, and it looks cleaner to me.

I there a cfg value I should be using that won't generate warnings, and would be clearer than my current method: `#[cfg(windows)]'

I would like to avoid adding a cfg item in the Cargo.toml

Please no comments about why this is a bad idea.

1 Like
#[cfg(any())]
7 Likes

And just as a note, there's a new and quickly progressing RFC to allow #[cfg(false)] for this purpose as well.

But unlike #if 0, #[cfg(any())] requires the decorated syntax to be well-formed. Defining a do-nothing macro (e.g. macro_rules! m {($($t:tt)*) => {}} and putting code within an invocation thereof reduces the requirement to that the ignored tokens are a lexically valid token stream with matched paren/brackets.

9 Likes

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.