In C++, you can undefine and redefine a macro. For example, a common thing to do in video games is to redefine the logging macro to nothing in Release mode. This guarantees that the code completely disappears which helps with performance.
Is there a way to do a similar thing in Rust?
Basically you might do:
This is how the macro
debug_assert!
is implemented. The doc says:This is the same as your situation, only for assert, not logging. Looking at the source:
This has also been briefly discussed on the Rust users forum, where the summary is that
cfg(debug_assertions)
is the way to check if we're in debug mode.I have no idea how stable
cfg(debug_assertions)
is, however.You would use conditional compilation attributes:
Here, you can use Cargo's "features" to provide a compile-time argument that switches the implementation of debugging.
However, there's no requirement to use macros in this case:
I'd trust pretty heavily in the optimizer to produce the same code in this case.