'How would i run a Rust program without a window only when it's in release mode [duplicate]
I have a Rust project I'm currently working on and I frequently have to debug and deploy it. While it's deployed it runs in the background without a window but when I'm debugging I want a window to see if there are any runtime errors.
I currently use #![windows_subsystem = "windows"]
and comment it out whenever I'm debugging, but is there a way for the program itself to check if it's in debug mode so it doesn't use that flag?
Solution 1:[1]
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_attribute(
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Frxstrem |