# Flush stdout in Rust

> Flush Rust stdout after print! so prompts appear immediately, and fix flush() by importing std::io::Write.

HTML: https://rustprint.com/output/flush-stdout/

Call `flush()` after `print!` when text must be visible before the next newline. Bring `std::io::Write` into scope first, because `flush()` is provided by that trait.

**Print a prompt and flush stdout immediately**

```rust
use std::io::{self, Write};

print!("Continue? [y/N] ");
io::stdout().flush().unwrap();
```

## Why print! may not appear immediately

Rust's standard output handle uses a shared buffer. When stdout is connected to a terminal, the standard library documents it as line-buffered: a newline normally flushes the buffered output automatically.

That is why `println!` usually appears immediately while a prompt written with `print!` can remain buffered. If the program then waits for input, the user may be staring at an invisible prompt.

For an interactive prompt, write the prompt and flush stdout before reading from stdin.

## Fix "no method named flush"

`flush()` comes from the `std::io::Write` trait. Importing `std::io` alone is not enough to make the trait method available.

**Calling stdout().flush() without std::io::Write in scope**

```rust
fn main() {
    std::io::stdout().flush().unwrap();
}
```

The failure above is compiled intentionally in CI. The fix is to bring the trait into scope before calling the method:

```rust
use std::io::Write;

std::io::stdout().flush()?;
```

The checked example above imports both `io` and `Write` together. Application code can propagate the `io::Result` with `?`; the example uses `unwrap()` only to keep the standalone snippet compact.

## When you do not need an explicit flush

Do not add `flush()` after every output call. Complete terminal lines written with `println!` normally benefit from line buffering already. Manual flushing matters when output must cross the buffer boundary before a newline, especially prompts, progress fragments, and interactive terminal output.

For the newline choice itself, see [`print!` vs `println!`](/output/print-and-println/index.md). For many repeated writes, see the [output macro overview](/output/output-macros/index.md) and its checked stdout-locking example.

## Sources

- [std::io::Stdout](https://doc.rust-lang.org/std/io/struct.Stdout.html)
- [std::io::Write::flush](https://doc.rust-lang.org/std/io/trait.Write.html#tymethod.flush)

## Related RustPrint guides

- [Rust print without newline: print! vs println!](/output/print-and-println/index.md): Print in Rust with or without a newline. Compare print! and println!, flush stdout for prompts, and avoid repeated stdout locking.
- [Rust output macros: print!, eprintln!, write!, and format!](/output/output-macros/index.md): Choose the right Rust output macro for stdout, stderr, an owned String, or an existing writer, including newline, flushing, and buffering behavior.
- [Rust write! and writeln! - format into strings, files, and buffers](/output/write-and-writeln/index.md): Use Rust write! and writeln! to format directly into String buffers, byte writers, files, sockets, and other write_fmt destinations.
