coding-horror/basic-computer-games · warning

closing

Error message

closing

What it means

A panic via .expect("closing") on io::stdin().read_line() at the end of the Amazing maze program, which prints 'press ENTER to exit' and waits so a compiled .exe window doesn't close immediately. The expect panics if stdin cannot be read at that final pause (closed stdin, EOF, broken pipe). The label 'closing' is just the panic message, not a descriptive error.

Source

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

    println!(".");
    //rest of maze
    for r in 0..height {
        print!("I");
        for c in 0..width {
            if walls[r][c]<2 {print!("  I");}
            else {print!("   ");}
        }
        println!();
        for c in 0..width {
            if walls[r][c] == 0 || walls[r][c]==2 {print!(":--");}
            else {print!(":  ");}
        }
        println!(".");
    }

    //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>() {

View on GitHub (pinned to 5301155192)

Solutions

  1. If piping input, include a trailing newline so the final read_line returns Ok.
  2. Run interactively so stdin stays open until you press ENTER.
  3. Replace .expect("closing") with `let _ = io::stdin().read_line(...)` to ignore read failure at the exit pause.

Example fix

// before
io::stdin().read_line(&mut String::new()).expect("closing");

// after: tolerate EOF at the exit prompt
let _ = io::stdin().read_line(&mut String::new());
Defensive patterns

Strategy: fallback

Validate before calling

// No validation needed; the read is a best-effort pause. Just ignore its result.

Try / catch

// Discard the result so EOF doesn't panic
let _ = io::stdin().read_line(&mut String::new());

Prevention

When it happens

Trigger: The program finishes drawing the maze and reaches the 'press ENTER to exit' pause, but stdin is closed (e.g. input was fully consumed from a pipe, or stdin is /dev/null, or the pipe broke).

Common situations: Piping all game input and letting the stream end before the final ENTER; running with `< /dev/null`; a GUI-launched .exe whose stdin handle is invalid.

Related errors


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