How to check if a project is built in release or debug mode using cfg in Rust?
Hello everyone! I just started learning Rust and am writing a small project with Cargo. I have a problem: I need the program to output a lot of debug information to the console in debug builds (e.g., logs with TRACE level), but in release builds these messages should not be output at all to avoid slowing down the program.
I know that in C/C++ preprocessor directives like `#ifdef DEBUG` are used for this. In Rust I've heard about the `cfg!()` macro, but I don't understand how to properly apply it for this specific task.
Here's an example of my current code:
```rust
fn main() {
let x = 42;
println!("x = {}", x);
// Here I want to add debug output that will only appear in debug builds
// println!("[DEBUG] x is: {}", x); // This should only work in debug
}
```
No answers yet.
Log in to leave an answer