coding-horror/basic-computer-games · error

CANNOT READ INPUT!

Error message

CANNOT READ INPUT!

What it means

A panic via .expect("CANNOT READ INPUT!") on io::stdin().read_line() inside the input loop of get_user_input() in the Amazing Rust port, which reads maze dimensions. read_line fails only on I/O-level errors (closed stdin, EOF, broken pipe), not on non-numeric input (that is handled by the parse match with a re-prompt). The expect aborts the process on any read failure.

Source

Thrown at 02_Amazing/rust/src/main.rs:183

    //stops the program from ending until you give input, useful when running a compiled .exe
    println!("\n\npress ENTER to exit");
    io::stdin().read_line(&mut String::new()).expect("closing");
}

fn get_user_input(prompt: &str) -> usize {
    //DATA
    let mut raw_input = String::new(); // temporary variable for user input that can be parsed later

    //input loop
    return loop {

        //print prompt
        println!("{}", prompt);

        //read user input from standard input, and store it to raw_input
        raw_input.clear(); //clear input
        io::stdin().read_line(&mut raw_input).expect( "CANNOT READ INPUT!");

        //from input, try to read a number
        match raw_input.trim().parse::<usize>() {
            Ok(i) => {
                if i>1 { //min size 1
                    break i; // this escapes the loop, returning i
                }
                else {
                    println!("INPUT OUT OF RANGE.  TRY AGAIN.");
                    continue;// run the loop again
                }
            }
            Err(e) => {
                println!("MEANINGLESS DIMENSION.  TRY AGAIN.  {}", e.to_string().to_uppercase());
                continue; // run the loop again
            }
        };
    }

View on GitHub (pinned to 5301155192)

Solutions

  1. Supply at least one numeric line per dimension prompt on stdin.
  2. Run interactively in a terminal with an open stdin.
  3. Replace .expect with a match that breaks the loop and exits cleanly on read error.

Example fix

// before
io::stdin().read_line(&mut raw_input).expect("CANNOT READ INPUT!");

// after
if io::stdin().read_line(&mut raw_input).is_err() {
    println!("\nUnable to read input. Exiting.");
    std::process::exit(1);
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure stdin is open / has enough lines before prompting
use std::io::IsTerminal;
if !std::io::stdin().is_terminal() {
    // caller must provide at least one numeric line per dimension prompt
}

Try / catch

// Break the loop and exit on read error
match io::stdin().read_line(&mut raw_input) {
    Ok(_) => { /* parse */ }
    Err(_) => { println!("Input unavailable."); std::process::exit(1); }
}

Prevention

When it happens

Trigger: stdin closed or pipe exhausted before the width/height prompt is answered; running with `< /dev/null`; an input fixture that ends before both dimensions are supplied.

Common situations: Automated runs where stdin is redirected and runs out of lines; a test harness closing stdin early; running the maze game headless without a TTY.

Related errors


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