How to check if a project is built in debug or release mode using cfg in Rust?
Hi everyone! I just started learning Rust and am trying to understand conditional compilation. I need different code to execute depending on whether I build the project in debug or release mode. For example, in debug I want to output a lot of debugging information, and in release — only minimal logs. I know that in C/C++ there are macros like `#ifdef DEBUG`, but how do I do this in Rust?
I tried using `#[cfg(debug_assertions)]`, but I don't fully understand how it works. Here's an example of my code:
```rust
fn main() {
#[cfg(debug_assertions)]
println!("Debug build");
#[cfg(not(debug_assertions))]
println!("Release build");
}
```
This code works, but I've heard that `debug_assertions` may behave unexpectedly in some cases. For example, if I run
No answers yet.
Log in to leave an answer
Blogs