{"record":{"id":"721716e466dae9b5","repo":"coding-horror/basic-computer-games","slug":"failed-to-read-the-line-721716","errorCode":null,"errorMessage":"Failed to read the line","messagePattern":"Failed to read the line","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"47_Hi-Lo/rust/src/main.rs","lineNumber":26,"sourceCode":"    );\n    println!(\"THIS IS THE GAME OF HI LO.\\n\");\n    println!(\"YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE\");\n    println!(\"HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS.  IF YOU\");\n    println!(\"GUESS THE AMOUNT, YOU WIN ALL THE MONEY IN THE JACKPOT!\");\n    println!(\"THEN YOU GET ANOTHER CHANCE TO WIN MORE MONEY.  HOWEVER,\");\n    println!(\"IF YOU DO NOT GUESS THE AMOUNT, THE GAME ENDS.\\n\");\n\n    let mut total: u32 = 0;\n    loop {\n        let jackpot_amount = rand::thread_rng().gen_range(1..101); // generates a random number between 1 and 100\n        for i in 0..6 {\n            println!(\"YOUR GUESS?\");\n\n            let mut guess = String::new();\n\n            io::stdin()\n                .read_line(&mut guess)\n                .expect(\"Failed to read the line\");\n\n            // this converts the input string into unsigned 32bit number and if the input entered is not a number\n            // it will again prompt the user to enter the guess number\n            let guess: u32 = match guess.trim().parse() {\n                Ok(num) => num,\n                Err(_) => {\n                    println!(\"PLEASE ENTER A NUMBER VALUE.\\n\");\n                    continue;\n                }\n            };\n\n            // compare it with the jackpot amount\n            if guess == jackpot_amount {\n                println!(\"\\nGOT IT!!!!!!!!!!   YOU WIN {} DOLLARS.\", jackpot_amount);\n                total += jackpot_amount;\n                println!(\"YOUR TOTAL WINNINGS ARE NOW {} DOLLARS.\\n\", total);\n                break;\n            } else if guess < jackpot_amount {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/47_Hi-Lo/rust/src/main.rs#L8-L44","documentation":"In 47_Hi-Lo, inside a loop of up to 6 guesses, the game reads the player's numeric guess via io::stdin().read_line(&mut guess).expect(\"Failed to read the line\"). Non-numeric input is handled gracefully by a match on guess.trim().parse() that prints 'PLEASE ENTER A NUMBER VALUE' and continues the loop. The .expect() guards only the I/O layer.","triggerScenarios":"The player is prompted 'YOUR GUESS?' and stdin returns Err or EOF. Invalid numbers trigger the retry loop, but missing input (EOF) panics before the parse match runs.","commonSituations":"Piped input that runs out during the 6-guess loop, Ctrl+D at a guess prompt, or CI tests that provide fewer lines than the game expects.","solutions":["Replace .expect() with .unwrap_or_default() so I/O failure yields an empty string, which the existing parse-failure path handles by printing the error and continuing","Use match to detect Ok(0) (EOF) and break the inner guess loop","Extract a read_guess helper that loops on parse errors but exits on I/O errors"],"exampleFix":"// before\nlet mut guess = String::new();\nio::stdin()\n    .read_line(&mut guess)\n    .expect(\"Failed to read the line\");\n\nlet guess: u32 = match guess.trim().parse() {\n\n// after\nlet mut guess = String::new();\nlet _ = io::stdin().read_line(&mut guess);\n\nlet guess: u32 = match guess.trim().parse() {","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"let mut guess = String::new();\nlet _ = io::stdin().read_line(&mut guess);\n// empty string fails to parse as u32, triggering the\n// existing 'PLEASE ENTER A NUMBER VALUE' retry","preventionTips":["For guess loops that already retry on parse failures, let I/O failures degrade to parse failures by using let _ = read_line(...)","Do not use .expect() on reads inside retry loops — the loop's error path should handle all failure modes","Test guess loops with input files that end mid-loop to verify graceful degradation"],"tags":["rust","stdin","read-line","panic","expect","io-error","eof","interactive","game-loop","cli"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}