# How do I stop a black console window opening behind my Windows GUI app?
Source: https://ruststudio.org/community/t/260/how-do-i-stop-a-black-console-window-opening-behind-my-windows-gui-app
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11; built in debug and release, run, and the Subsystem field of each executable's header was read (2 for the GUI subsystem, 3 for console).
Category: Help and questions | Kind: FAQ | Tags: windows, console, cfg, desktop | Views: 0

## Short answer
A Windows executable is built for either the console or the GUI subsystem. Add #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] at the top of main.rs, so release builds open no console while debug builds keep one for println. The subsystem field of both builds was read from the files.

## Answer
## Answer
Windows marks every program with a **subsystem**. A *console* program gets a console window (or shares the one it was started from). A *GUI* program gets none. Rust builds console programs by default, so a double-clicked GUI app also opens a black window.

One line at the top of the crate root changes that:

```rust title=src/main.rs
// Release builds on Windows get the GUI subsystem (no console window); debug builds keep the console for println!.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
    println!("hello"); // visible only when the console exists
}
```

`cfg_attr(condition, attribute)` applies the attribute only when the condition holds, here "not a debug build". On macOS and Linux the `windows_subsystem` attribute has no effect, so the line is harmless there.

What was checked: both builds were made, run, and the `Subsystem` field in the PE header of each executable was read. The debug build has value 3 (console) and printed `hello`; the release build has value 2 (GUI) and printed nothing.

Side effects of the GUI subsystem:

- **`println!` output goes nowhere**, and panics print no message anywhere a user can see it. Log to a file in the app's data folder (see the settings thread), and show a message box or error banner for fatal errors.
- A program started from a terminal does not print into that terminal. Windows offers a way to attach to the parent's console (`AttachConsole` in the Windows API); it needs a call into the Windows API.
- Command-line tools that share the code should be a separate binary in the same package, built as a console program.

## Common mistakes
- Applying `windows_subsystem` to a debug build and then wondering why `println!` and panic messages vanish. The `cfg_attr` line above avoids it.
- Putting the attribute in a module. It belongs at the crate root (the top of `main.rs`) and needs the `#!` form.
- Using it for a service or a tool that must print: they need the console subsystem.

## Related questions
### Does it also set the icon or version information?
No. Those are resources embedded in the executable; a `build.rs` with a resource crate does it. This thread covers only the console window.

## Sources
- The Rust Reference, `windows_subsystem` attribute: https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute (read 2026-12-05)
- The Rust Reference, `cfg_attr`: https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute (read 2026-12-05)
