You can use debug_assertions as the corresponding configuration flag. It works both with the attributes #[cfg(...)] and with cfg! macro:
#[cfg(debug_assertions)] fn example() { println!("Debugging enabled"); } #[cfg(not(debug_assertions))] fn example() { println!("Debugging disabled"); } fn main() { if cfg!(debug_assertions) { println!("Debugging enabled"); } else { println!("Debugging disabled"); } #[cfg(debug_assertions)] println!("Debugging enabled"); #[cfg(not(debug_assertions))] println!("Debugging disabled"); example(); }
This configuration flag has been named as the correct way to do this in this discussion . No more suitable built-in conditions.
From the link :
debug_assertions - debug_assertions by default when compiling without optimization. This can be used to include additional debugging code in development, but not in production. For example, it controls the behavior of the standard debug_assert! library debug_assert! macro.
An alternative, slightly more complicated way is to use #[cfg(feature = "debug")] and create a build script that includes a "debug" function for your mailbox, as shown here .
source share