coding-horror/basic-computer-games · warning

Failed to read the line

Error message

Failed to read the line

What it means

A panic via .expect("Failed to read the line") on io::stdin().read_line() in the Buzzword Rust port, used at the end of each generated phrase to ask whether the user wants another phrase. read_line returns Err only on I/O-level failure (closed stdin, EOF, broken pipe); the Yes/No parsing is done afterward on the String. The expect aborts the process if stdin cannot be read at the continue prompt.

Source

Thrown at 20_Buzzword/rust/src/main.rs:80

    let mut continue_running: bool = true;

    while continue_running {
        let mut first_word: bool = true;
        for section in &words {
            if !first_word {
                print!(" ");
            }
            first_word = false;
            print!("{}", section.choose(&mut rand::thread_rng()).unwrap());
        }
        print!("\n\n? ");
        io::stdout().flush().unwrap();

        let mut cont_question: String = String::new();
        io::stdin()
            .read_line(&mut cont_question)
            .expect("Failed to read the line");
        if !cont_question.to_uppercase().starts_with("Y") {
            continue_running = false;
        }
    }
    println!("Come back when you need help with another report!\n");

}


/////////////////////////////////////////////////////////////////////////
//
// Porting Notes
//
//   The original program stored all 39 words in one array, then
//   built the buzzword phrases by randomly sampling from each of the
//   three regions of the array (1-13, 14-26, and 27-39).
//
//   Here, we're storing the words for each section in separate

View on GitHub (pinned to 5301155192)

Solutions

  1. Append a Y/N line in piped input for each continue prompt.
  2. Run interactively.
  3. Replace .expect with `match` that sets continue_running=false on read error and exits gracefully.

Example fix

// before
io::stdin().read_line(&mut cont_question).expect("Failed to read the line");

// after
if io::stdin().read_line(&mut cont_question).is_err() {
    continue_running = false;
}
Defensive patterns

Strategy: fallback

Validate before calling

// The continue answer can default to 'stop' on read failure; no pre-check needed.

Try / catch

// On read error, end the loop gracefully
match io::stdin().read_line(&mut cont_question) {
    Ok(_) => { if !cont_question.to_uppercase().starts_with('Y') { continue_running = false; } }
    Err(_) => { continue_running = false; }
}

Prevention

When it happens

Trigger: stdin closed or exhausted before the '? continue (Y/N)' prompt; piped input that ends before the user answers; running with `< /dev/null`.

Common situations: Piping a fixture without a trailing Y/N line; headless/CI runs with no open stdin; a feeder that closes stdin after generating one phrase's worth of input.

Related errors


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