{"record":{"id":"972892e616076ef4","repo":"coding-horror/basic-computer-games","slug":"cannot-read-input-972892","errorCode":null,"errorMessage":"CANNOT READ INPUT!","messagePattern":"CANNOT READ INPUT!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"02_Amazing/rust/src/main.rs","lineNumber":183,"sourceCode":"\n    //stops the program from ending until you give input, useful when running a compiled .exe\n    println!(\"\\n\\npress ENTER to exit\");\n    io::stdin().read_line(&mut String::new()).expect(\"closing\");\n}\n\nfn get_user_input(prompt: &str) -> usize {\n    //DATA\n    let mut raw_input = String::new(); // temporary variable for user input that can be parsed later\n\n    //input loop\n    return loop {\n\n        //print prompt\n        println!(\"{}\", prompt);\n\n        //read user input from standard input, and store it to raw_input\n        raw_input.clear(); //clear input\n        io::stdin().read_line(&mut raw_input).expect( \"CANNOT READ INPUT!\");\n\n        //from input, try to read a number\n        match raw_input.trim().parse::<usize>() {\n            Ok(i) => {\n                if i>1 { //min size 1\n                    break i; // this escapes the loop, returning i\n                }\n                else {\n                    println!(\"INPUT OUT OF RANGE.  TRY AGAIN.\");\n                    continue;// run the loop again\n                }\n            }\n            Err(e) => {\n                println!(\"MEANINGLESS DIMENSION.  TRY AGAIN.  {}\", e.to_string().to_uppercase());\n                continue; // run the loop again\n            }\n        };\n    }","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/02_Amazing/rust/src/main.rs#L165-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Supply at least one numeric line per dimension prompt on stdin.","Run interactively in a terminal with an open stdin.","Replace .expect with a match that breaks the loop and exits cleanly on read error."],"exampleFix":"// before\nio::stdin().read_line(&mut raw_input).expect(\"CANNOT READ INPUT!\");\n\n// after\nif io::stdin().read_line(&mut raw_input).is_err() {\n    println!(\"\\nUnable to read input. Exiting.\");\n    std::process::exit(1);\n}","handlingStrategy":"try-catch","validationCode":"// Ensure stdin is open / has enough lines before prompting\nuse std::io::IsTerminal;\nif !std::io::stdin().is_terminal() {\n    // caller must provide at least one numeric line per dimension prompt\n}","typeGuard":null,"tryCatchPattern":"// Break the loop and exit on read error\nmatch io::stdin().read_line(&mut raw_input) {\n    Ok(_) => { /* parse */ }\n    Err(_) => { println!(\"Input unavailable.\"); std::process::exit(1); }\n}","preventionTips":["Avoid .expect on read_line; match the Result.","Provide a numeric line per prompt when piping.","Guard the input loop with an EOF check."],"tags":["rust","stdin","io","panic","expect"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}