{"record":{"id":"101cc9d07cf38644","repo":"coding-horror/basic-computer-games","slug":"error-reading-line-101cc9","errorCode":null,"errorMessage":"Error reading line!","messagePattern":"Error reading line!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"76_Russian_Roulette/rust/src/main.rs","lineNumber":27,"sourceCode":"    println!(\"HERE IS A REVOLVER.\");\n\n    loop {\n        println!(\"TYPE '1' TO SPIN CHAMBER AND PULL TRIGGER\");\n        println!(\"TYPE '2' TO GIVE UP.\");\n        println!(\"GO\");\n\n        let mut tries = 0;\n\n        loop {\n            let mut pull_trigger = true;\n\n            loop {\n                println!(\"?\");\n                let mut input = String::new();\n\n                std::io::stdin()\n                    .read_line(&mut input)\n                    .expect(\"Error reading line!\");\n\n                match input.trim() {\n                    \"1\" => break,\n                    \"2\" => {\n                        pull_trigger = false;\n                        break;\n                    }\n                    _ => println!(\"Invalid input.\"),\n                }\n            }\n\n            if pull_trigger {\n                std::thread::sleep(Duration::from_secs(1));\n\n                match rand::thread_rng().gen_range(0..6) {\n                    0 => {\n                        println!(\"\\tBANG!!!!!   YOU'RE DEAD!\");\n                        println!(\"CONDOLENCES WILL BE SENT TO YOUR RELATIVES.\");","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/76_Russian_Roulette/rust/src/main.rs#L9-L45","documentation":"This panic is triggered by `.expect(\"Error reading line!\")` on `std::io::stdin().read_line()` inside the trigger-pull input loop of Russian Roulette (line 27). The loop reads `\"1\"` or `\"2\"` from the player to decide whether to pull the trigger. The `_` arm handles invalid *content*, but `.expect` crashes on I/O-level failures.","triggerScenarios":"Stdin returns `Err` or EOF while the player is at the trigger-pull prompt. The input match (`\"1\" => break`, `\"2\" => pull_trigger = false; break`, `_ => println!`) only runs after a successful read, so malformed input is retried while stream failure crashes.","commonSituations":"Non-interactive execution. Piped input file with too few lines. SSH drop. Process launched without a TTY.","solutions":["Replace `.expect` with a `match` that exits gracefully on EOF (`Ok(0)`) and retries or exits on `Err`.","On EOF, break the trigger loop and end the game with a summary message.","Provide piped input that covers all trigger-pull prompts for a full game session."],"exampleFix":"// before\nstd::io::stdin().read_line(&mut input).expect(\"Error reading line!\");\n\n// after\nmatch std::io::stdin().read_line(&mut input) {\n    Ok(0) => { println!(\"\\nGame interrupted.\"); return; }\n    Ok(_) => {}\n    Err(e) => { eprintln!(\"Input error: {}\", e); return; }\n}","handlingStrategy":"try-catch","validationCode":"use std::io::IsTerminal;\nif !io::stdin().is_terminal() {\n    // Provide input via pipe or file\n}","typeGuard":"fn read_choice() -> Option<String> {\n    let mut input = String::new();\n    match std::io::stdin().read_line(&mut input) {\n        Ok(0) | Err(_) => None,\n        Ok(_) => Some(input),\n    }\n}","tryCatchPattern":"match std::io::stdin().read_line(&mut input) {\n    Ok(0) => { println!(\"\\nGame interrupted.\"); return; }\n    Ok(_) => { /* match on \"1\" / \"2\" / _ */ }\n    Err(e) => { eprintln!(\"{}\", e); return; }\n}","preventionTips":["Handle EOF in input loops as a clean game exit, not a crash.","Distinguish content validation (retry) from I/O failure (exit) in loop design.","Provide piped input files with enough lines for all trigger-pull prompts."],"tags":["rust","stdin","io","panic","expect","cli-game"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}