{"record":{"id":"1fbc728df94ec93e","repo":"coding-horror/basic-computer-games","slug":"failed-to-flush-to-stdout","errorCode":null,"errorMessage":"Failed to flush to stdout.","messagePattern":"Failed to flush to stdout\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"10_Blackjack/rust/src/main.rs","lineNumber":570,"sourceCode":"\n    NOTE:'/' (splitting) is not currently implemented, and does nothing\n\n    PRESS ENTER TO CONTINUE\n    \");\n    io::stdin().read_line(&mut String::new()).expect(\"Failed to read line\");\n}\n\n/**\n * gets a usize integer from user input\n */\nfn get_number_from_user_input(prompt: &str, min:usize, max:usize) -> usize {\n    //input loop\n    return loop {\n        let mut raw_input = String::new(); // temporary variable for user input that can be parsed later\n\n        //print prompt\n        println!(\"{}\", prompt);\n        stdout().flush().expect(\"Failed to flush to stdout.\");\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 < min || i > max { //input out of desired range\n                    println!(\"INPUT OUT OF VALID RANGE.  TRY AGAIN.  {}-{}\",min,max);\n                    continue; // run the loop again\n                }\n                else {\n                    break i;// this escapes the loop, returning i\n                }\n            },\n            Err(e) => {\n                println!(\"INVALID INPUT.  TRY AGAIN.  {}\", e.to_string().to_uppercase());\n                continue; // run the loop again","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/10_Blackjack/rust/src/main.rs#L552-L588","documentation":"A panic via .expect(\"Failed to flush to stdout.\") on stdout().flush() at the top of the input loop in get_number_from_user_input() of the Blackjack Rust port. flush returns Err when stdout cannot be written (closed stdout, broken pipe, disk full on redirect). Because a print! without newline needs a flush before read_line, this call ensures the prompt appears; on flush failure it panics.","triggerScenarios":"stdout is closed or piped to a consumer that exited (SIGPIPE/broken pipe); redirecting output to a full disk or unwritable file; running under a harness that closes stdout.","commonSituations":"Piping the program's output to `head` which closes the pipe early; redirecting to a filesystem that fills up; a wrapper that closes stdout prematurely.","solutions":["Avoid piping stdout to a tool that closes the pipe before the game finishes; run interactively or log to a file with adequate space.","Replace .expect with `let _ = stdout().flush();` to tolerate flush failure.","If scripting, ensure the downstream consumer reads all output."],"exampleFix":"// before\nstdout().flush().expect(\"Failed to flush to stdout.\");\n\n// after\nlet _ = stdout().flush();","handlingStrategy":"fallback","validationCode":"// Avoid piping stdout through an early-closing consumer; check writability is unnecessary if you discard the flush result.","typeGuard":null,"tryCatchPattern":"// Tolerate flush failure (broken pipe) instead of panicking\nlet _ = stdout().flush();\n// or handle SIGPIPE/BrokenPipe explicitly:\nmatch stdout().flush() { Ok(_) => {}, Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => std::process::exit(0), Err(_) => {} }","preventionTips":["Do not pipe program output through `head`/`tail` that close the pipe early.","Use `let _ = stdout().flush()` for non-critical flushes.","Install a SIGPIPE handler or check ErrorKind::BrokenPipe when redirecting."],"tags":["rust","stdout","io","panic","expect","flush"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}