How to check release / debug builds using cfg in Rust?

33,528

You can use debug_assertions as the appropriate configuration flag. It works with both #[cfg(...)] attributes and the 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 was named as a correct way to do this in this discussion. There is no more suitable built-in condition for now.

From the reference:

debug_assertions - Enabled by default when compiling without optimizations. This can be used to enable extra debugging code in development but not in production. For example, it controls the behavior of the standard library's debug_assert! macro.

An alternative, slightly more complicated way, is to use #[cfg(feature = "debug")] and create a build script that enables a "debug" feature for your crate, as shown here.

Share:
33,528

Related videos on Youtube

ideasman42
Author by

ideasman42

Updated on July 31, 2020

Comments

  • ideasman42
    ideasman42 over 3 years

    With the C pre-processor it's common to do,

    #if defined(NDEBUG)
        // release build
    #endif
    
    #if defined(DEBUG)
        // debug build
    #endif
    

    Cargo's rough equivalents are:

    • cargo build --release for release.
    • cargo build for debug.

    How would Rust's #[cfg(...)] attribute or cfg!(...) macro be used to do something similar?

    I understand that Rust's pre-processor doesn't work like C's. I checked the documentation and this page lists some attributes. (assuming this list is comprehensive)

    debug_assertions could be checked, but it may be misleading when used to check for the more general debugging case.

    I'm not sure if this question should be related to Cargo or not.

    • antoyo
      antoyo over 7 years
      @Shepmaster This is similar to this question.
  • turboladen
    turboladen over 4 years
    Just for the record, this doesn't work in build scripts. Just finding this out for myself so thought I'd share.
  • BingLi224
    BingLi224 about 4 years
    if it's #[cfg(debug_assertions)] instead of #[cfg(debug_assertions)] {...}, there'll be error[E0658]: attributes on expressions are experimental and error: removing an expression is not supported in this position. Rust 1.42.0