Conditional Compilation if unoptimized

#1

Hi,

Is there a cfg attribute or macro to compile a code section if we are unoptimized, in other words, exclude it if we are in release.

I’m thinking about an ideas like:

if cfg!(unoptimized) {
   println!("Configuration:{:?}",config);
}

Thanks

1 Like
#2

Hi, you can use cfg!(debug_assertions), it’s what debug_assert!() uses. I don’t know if there is a more general flag.

1 Like
#3

There’s no cfg for optimisations. There’s one for debug assertions that is abused to mean debug builds in general.

However, for printing stuff conditionally, it’s best to use the log crate. It has debug and trace macros that can be complied optionally.

2 Likes
#4

Thanks concerning log you are right but in that case it’s at beginning before log is initialized.