coding-horror/basic-computer-games · error

**Failed to read line**

Error message

**Failed to read line**

What it means

In 51_Hurkle, get_guess reads a comma-separated coordinate pair (e.g., '5,3') via io::stdin().read_line(&mut input).expect("**Failed to read line**"). The input is split on commas and each axis is parsed as u8, with parse failures handled by printing an error and continuing the guess loop. The .expect() panics only on I/O-level failure.

Source

Thrown at 51_Hurkle/rust/src/game.rs:41

            println!("SORRY, THAT'S {} GUESSES.", self.tries);
            println!("THE HURKLE IS AT {}, {}", self.hurkle.0, self.hurkle.1);
            return true;
        }
        self.tries += 1;
        self.process_guess(self.get_guess())
    }

    fn get_guess(&self) -> Position {
        let mut pos = (0, 0);

        'guess: loop {
            println!("GUESS # {}?", self.tries);

            let mut input = String::new();

            io::stdin()
                .read_line(&mut input)
                .expect("**Failed to read line**");

            let input: Vec<&str> = input.trim().split(",").collect();

            let mut is_y = false;
            for a in input {
                match a.parse::<u8>() {
                    Ok(a) => {
                        if a > 10 || a == 0 {
                            println!("GUESS AXIS CANNOT BE ZERO OR LARGER THAN TEN!");
                            break;
                        }
                        if is_y {
                            pos.1 = a;
                            break 'guess;
                        } else {
                            pos.0 = a;
                            is_y = true;
                        }

View on GitHub (pinned to 5301155192)

Solutions

  1. Replace .expect() with .unwrap_or_default() so I/O failure yields an empty string, which the comma-split produces an empty vec that the validation loop handles by re-prompting
  2. Use match to detect EOF and return a default Position to signal game-over
  3. Break the guess loop on Err

Example fix

// before
let mut input = String::new();
io::stdin()
    .read_line(&mut input)
    .expect("**Failed to read line**");

let input: Vec<&str> = input.trim().split(",").collect();

// after
let mut input = String::new();
let _ = io::stdin().read_line(&mut input);

let input: Vec<&str> = input.trim().split(",").collect();
Defensive patterns

Strategy: fallback

Try / catch

let mut input = String::new();
let _ = io::stdin().read_line(&mut input);
// empty string splits into a vec with one empty element,
// which fails parse::<u8>, triggering the retry loop

Prevention

When it happens

Trigger: The game prompts 'GUESS # N?' and stdin returns Err or EOF. Malformed coordinates (wrong separator, out-of-range values) are handled by the validation logic, but missing input is not.

Common situations: Piped input that exhausts before all 5 guesses are made, pressing Ctrl+D at a coordinate prompt, or CI tests with insufficient input lines.

Related errors


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