You can use #![cfg_attr(predicate, attribute)]
to only compile with an attribute if the specific predicate evaluates to true.
As for what the predicate is, my recommendation would be use an explicit feature (e.g. console
) that you set when you want to build with the non-default behavior (e.g. having the console window appear).
Additionally, if you want to have your code be portable, you should also check that you are compiling for Windows.
In Cargo.toml
:
[features]
console = []
In main.rs
:
#![cfg_attr(
all(
target_os = "windows",
not(feature = "console"),
),
windows_subsystem = "windows"
)]
Then you can compile or run with --features console
to have the default console
subsystem be used instead.
If you want to actually check for the debug mode, the closest you can get is the debug_assertions
predicate, which is enabled by default when compiling without optimizations.
In main.rs
:
#![cfg_attr(
all(
target_os = "windows",
not(debug_assertions),
),
windows_subsystem = "windows"
)]