std-type

BufReader: reduce small reads

Wrap a reader when consuming many small chunks or lines. The wrapper can read ahead; direct access to the underlying reader can skip bytes that are still buffered. Avoid assuming a fixed default capacity. Expected stdout (not executed): x Common mistake: Dropping a buffered reader and expecting unread buffered bytes to reappear in the underlying stream.

Formal Syntax & Structure

rust syntaxrust
BufReader::new(reader); BufReader::with_capacity(capacity, reader)

Working Example

Example Snippetrust
fn main() {    use std::io::{BufReader, BufRead};    let mut r = BufReader::with_capacity(4, &b"x    y    "[..]);    let mut line = String::new();    r.read_line(&mut line).unwrap();    println!("{}", line.trim_end());}
Rust Reference