std-type

BufWriter: batch output

Buffer small writes before forwarding them to the underlying writer. This can reduce calls to costly sinks; no speedup is guaranteed for a vector. Explicitly flush and handle errors before treating work as complete. Expected stdout (not executed): abcd Common mistake: Assuming the wrapper provides transactional writes.

Formal Syntax & Structure

rust syntaxrust
BufWriter::new(writer); BufWriter::with_capacity(capacity, writer)

Working Example

Example Snippetrust
fn main() {    use std::io::{BufWriter, Write};    let mut w = BufWriter::with_capacity(16, Vec::new());    w.write_all(b"ab").unwrap();    w.write_all(b"cd").unwrap();    w.flush().unwrap();    let bytes = w.into_inner().unwrap();    println!("{}", String::from_utf8(bytes).unwrap());}
Rust Reference