coding-horror/basic-computer-games · warning

failed to flush

Error message

failed to flush

What it means

A panic via .expect("failed to flush") on stdout().flush() in get_dollar_value_in_cents_from_user() of the Change (making change) Rust port. The prompt is printed without a trailing newline and flushed so it sits on the same line as the typed input; if stdout cannot be flushed (closed/broken pipe, unwritable redirect), the process panics before reading input.

Source

Thrown at 22_Change/rust/src/main.rs:129

 */
fn welcome() {
    println!("\t\t\t\tCHANGE\n\t      CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY\n\n\n");
}

/**
 * get number of money from user input
 */
fn get_dollar_value_in_cents_from_user(prompt:&str) -> i16 {
    let mut value:i16;
    //input loop
    loop {
        //data
        let mut raw_input = String::new();

        //print prompt
        print!("{}",prompt);
        //flush std out // allows prompt to be on same line as input
        stdout().flush().expect("failed to flush");

        //get input
        io::stdin().read_line(&mut raw_input).expect("failed to read input");
        //filter out characters that aren't numbers or '.'
        let mut no_prior_periods = true;
        raw_input = raw_input.chars().filter(|c| {
            if c.eq_ignore_ascii_case(&'.') && no_prior_periods {
                no_prior_periods = false;
                true
            } else {
                c.is_ascii_digit()
            }
        }).collect();

        //should only be (at most) 1 .
        if !raw_input.contains(".") { raw_input += ".00";} //if there are none, add one

        //ensure there are at least 2 trailing digits

View on GitHub (pinned to 5301155192)

Solutions

  1. Run interactively or redirect to a writable file with adequate space instead of piping through an early-closing consumer.
  2. Replace .expect with `let _ = stdout().flush();` to tolerate flush failure.
  3. Ensure the downstream reader consumes all output.

Example fix

// before
stdout().flush().expect("failed to flush");

// after
let _ = stdout().flush();
Defensive patterns

Strategy: fallback

Validate before calling

// Avoid piping through early-closing consumers; otherwise discard the flush result.

Try / catch

// Discard flush result to tolerate broken pipe
let _ = stdout().flush();

Prevention

When it happens

Trigger: stdout closed or piped to a consumer that exited (broken pipe/SIGPIPE); redirecting output to a full or read-only file; a parent process closing stdout.

Common situations: Piping output through `head`/`tail` that closes the pipe early; redirecting to a full filesystem; a supervisor that closes stdout.

Related errors


AI-assisted analysis of coding-horror/basic-computer-games@5301155192 (2026-08-13). Data as JSON: /api/errors/f89186fdd6a0f018. Report an issue: GitHub.