I'm wondering if something like this is possible
fn main() {
#[cfg(foo)] {
println!("using foo config");
}
}
The context is some code that cannot adequately be tested with just unit tests. I'll often have to run a "demo" cfg which displays information. I'm looking for alternatives to manually commenting in/out some portions of code.
As of at least Rust 1.21.1, it's possible to do this as exactly as you said:
Before this, it isn't possible to do this completely conditionally (i.e. avoiding the block being type checked entirely), doing that is covered by RFC #16. However, you can use the
cfg
macro which evaluates to eithertrue
orfalse
based on the--cfg
flags:The body of the
if
always has name-resolution and type checking run, so may not always work.You may interested in the crate cfg-if, or a simple macro will do: