std-method

BufRead::fill_buf and consume

Inspect bytes without copying, then consume only the prefix processed. The borrowed slice must not remain in use across the mutable consume call. An empty buffer from fill_buf indicates EOF for ordinary finite readers. Expected stdout (not executed): 3 c Common mistake: Keeping the borrowed slice alive while consuming the reader.

Formal Syntax & Structure

rust syntaxrust
reader.fill_buf() -> io::Result<&[u8]>; reader.consume(n)

Working Example

Example Snippetrust
fn main() {    use std::io::{BufRead, BufReader};    let mut r = BufReader::with_capacity(3, &b"abcdef"[..]);    let n = r.fill_buf().unwrap().len();    println!("{n}");    r.consume(2);    println!("{}", std::str::from_utf8(r.fill_buf().unwrap()).unwrap());}
Rust Reference