expression

Report a buffered output failure

A buffered write may appear successful before the destination is contacted. Explicitly flush and propagate failure before claiming success. The synthetic writer below always fails on write, making the error reproducible without a real device. Fixture: Expected stdout (not executed): output failed Expected stderr: (empty) Expected exit: 0 Common mistake: Relying on drop to report write errors. A successful flush also does not imply durable disk storage.

Formal Syntax & Structure

rust syntaxrust
writer.flush()?

Working Example

Example Snippetrust
fn main() {    use std::io::{self, Write, BufWriter};    struct Broken;    impl Write for Broken {        fn write(&mut self, _: &[u8]) -> io::Result<usize> {            Err(io::Error::new(io::ErrorKind::BrokenPipe, "demo"))        }        fn flush(&mut self) -> io::Result<()> { Ok(()) }    }    let mut output = BufWriter::with_capacity(64, Broken);    output.write_all(b"row\n").unwrap();    assert_eq!(output.flush().unwrap_err().kind(), io::ErrorKind::BrokenPipe);    println!("output failed");}
Rust Reference