{"record":{"id":"e1e20b12cdb6e55f","repo":"coding-horror/basic-computer-games","slug":"failed-to-read-input-e1e20b","errorCode":null,"errorMessage":"Failed to read input","messagePattern":"Failed to read input","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"95_Weekday/rust/src/main.rs","lineNumber":301,"sourceCode":"    WEEKDAY IS A COMPUTER DEMONSTRATION THAT\n    GIVES FACTS ABOUT A DATE OF INTEREST TO YOU.\n    \");\n}\n\n/**\n * gets a string from user input\n */\nfn get_str_from_user(prompt:&str) -> String {\n    //DATA\n    let mut raw_input = String::new();\n\n    //print prompt\n    print!(\"{}\",prompt);\n    //flust std out //allows prompt to be on same line as input\n    stdout().flush().expect(\"failed to flush\");\n\n    //get input and trim whitespaces\n    io::stdin().read_line(&mut raw_input).expect(\"Failed to read input\");\n\n    //return raw input\n    return raw_input.trim().to_string();\n}\n","sourceCodeStart":283,"sourceCodeEnd":306,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/95_Weekday/rust/src/main.rs#L283-L306","documentation":"The get_str_from_user helper for the Weekday calculator flushes stdout (so the prompt and input share a line) then calls read_line().expect(\"Failed to read input\"). The panic fires on io::Error from read_line. On EOF (Ok(0)) the function returns an empty trimmed string silently — the caller then receives \"\" which may cause downstream parse failures or logic errors in the weekday calculation.","triggerScenarios":"stdin pipe closed during date-component entry. A terminal session dropped while the program waits for input. Non-interactive execution where stdin provides no data.","commonSituations":"Scripting the Weekday calculator with piped date input that runs out of lines. Running in CI without interactive stdin. Testing with /dev/null as stdin.","solutions":["Change get_str_from_user to return Option<String> or Result<String, io::Error> instead of String; return None/Err on EOF or I/O error.","In the caller, check for empty string or None and re-prompt or exit with a clear message.","Replace .expect() with a match: Ok(0) => return None, Err(e) => return Err(e), Ok(_) => return Some(trimmed).","For testing, pipe every date component the program will request."],"exampleFix":"// before\nfn get_str_from_user(prompt:&str) -> String {\n    // ...\n    io::stdin().read_line(&mut raw_input).expect(\"Failed to read input\");\n    return raw_input.trim().to_string();\n}\n\n// after\nfn get_str_from_user(prompt:&str) -> Option<String> {\n    // ...\n    match io::stdin().read_line(&mut raw_input) {\n        Ok(0) => return None,\n        Ok(_) => return Some(raw_input.trim().to_string()),\n        Err(e) => {\n            eprintln!(\"Input error: {e}\");\n            return None;\n        }\n    }\n}","handlingStrategy":"try-catch","validationCode":"// Return Option<String> so callers can detect missing input\nfn get_str_from_user(prompt: &str) -> Option<String> {\n    let mut raw_input = String::new();\n    print!(\"{}\", prompt);\n    stdout().flush().ok()?;\n    match io::stdin().read_line(&mut raw_input) {\n        Ok(0) => None,\n        Ok(_) => Some(raw_input.trim().to_string()),\n        Err(_) => None,\n    }\n}","typeGuard":null,"tryCatchPattern":"// Caller side: handle None explicitly\nlet date_str = match get_str_from_user(\"Enter date: \") {\n    Some(s) if !s.is_empty() => s,\n    _ => {\n        println!(\"No input received. Exiting.\");\n        return;\n    }\n};","preventionTips":["Return Option or Result from input helpers instead of String — String cannot represent 'no input'.","Callers of get_str_from_user should check for empty strings since the current version returns \"\" on EOF.","Test the Weekday calculator with /dev/null stdin to catch silent empty-string cascades.","Flush stdout before every read_line in prompt-then-read patterns (already done here)."],"tags":["rust","io","stdin","panic","string-input","weekday","silent-eof"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}