On this page
BufReader
Prev: `File` | Up: io_and_fs | Next: `PathBuf`
Rel: io_and_fs, rust/builtins
Ref: Rust std — BufReader
Tags: #public #prog #rust #stdlib #io_and_fs #tier/core
Signature & Overview
BufReader<R: Read>
Adds an in-memory buffer and .lines() iterator over any Read stream.
Notes & Pitfalls
- Avoids a syscall per byte/line: Always wrap
FileinBufReaderbefore calling.lines()or.read_line(). - Reuse a single
Stringbuffer with.read_line(&mut buf)(rememberingbuf.clear()) for zero-allocation line loops.
Example
use std::io::{BufRead, BufReader};
fn main() {
let data = b"alpha
beta
gamma
";
let reader = BufReader::new(&data[..]);
let lines: Vec<String> = reader.lines().map(Result::unwrap).collect();
assert_eq!(lines, vec!["alpha", "beta", "gamma"]);
}
Linked from
Related notes
- tuts/rust/std/io_and_fs/File Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/io_and_fs/PathBuf Linked here; links to this page; shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/io_and_fs Linked here; links to this page; shared tags: #prog, #rust, #stdlib
- tuts/rust/builtins Linked here; shared tags: #prog, #rust
- tuts/rust/tutorials/cli_file_processor_with_result links to this page; shared tags: #prog, #rust
- tuts/rust/std/collections/BinaryHeap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/BTreeMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashMap shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/HashSet shared tags: #prog, #rust, #stdlib, #tier
- tuts/rust/std/collections/VecDeque shared tags: #prog, #rust, #stdlib, #tier