Conditional compilation for debug/release



I’m building a small tool to detect code injection in Unity assemblies, and I’d like to use compiler directives for debug and release builds. Something like;

#[cfg(build = "debug")]
fn do_something(){
// Output sensitive debugging info

#[cfg(build = "release")]
fn do_something(){
// Don't output anything

Is there a way to do this in Rust? I’ve searched around the web, but I guess I’m missing some fundamentally different concept in Rust.

Best regards,

Conditional Returns in test build

Still looking for a good way to do this. I’m surprised nobody else is interested in conditional compilation for debug and release builds.


You can use cfg(debug_assertions) but there should probably be a cfg(debug).


Thanks. How did you know about “debug_assertions”? I read through the reference and doco but never came across that one.


I looked at the source code for the debug_assert macro (so this cfg may not be very stable).


There really should be a #[cfg(debug)] currently you can define something like #[cfg(feature= "debug")] but you than have to manually add --features debug to your cargo build.

On the other hand, I can see why you would want an explicit call.

edit: hihi, I misjudged the age of this thread, reading “Apr’15” I though this was only 3 days old :stuck_out_tongue:


You can use the following build script

use std::env;

pub fn main() {
    if let Ok(profile) = env::var("PROFILE") {
        println!("cargo:rustc-cfg=build={:?}", profile);


The debug_assertions config option seems to be the legit way to do this.

It is mentioned in the rust reference:
And it is mentioned in the cargo docs: