Skip to content

Commit 83e8305

Browse files
authoredAug 28, 2022
Rollup merge of #100520 - jakubdabek:patch-1, r=thomcc
Add mention of `BufReader` in `Read::bytes` docs There is a general paragraph about `BufRead` in the `Read` trait's docs, however using `bytes` without `BufRead` *always* has a large impact, due to reads of size 1. `@rustbot` label +A-docs
2 parents 58174e3 + 8509936 commit 83e8305

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed
 

‎library/std/src/io/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,10 @@ pub trait Read {
887887
/// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
888888
/// otherwise. EOF is mapped to returning [`None`] from this iterator.
889889
///
890+
/// The default implementation calls `read` for each byte,
891+
/// which can be very inefficient for data that's not in memory,
892+
/// such as [`File`]. Consider using a [`BufReader`] in such cases.
893+
///
890894
/// # Examples
891895
///
892896
/// [`File`]s implement `Read`:
@@ -899,10 +903,11 @@ pub trait Read {
899903
/// ```no_run
900904
/// use std::io;
901905
/// use std::io::prelude::*;
906+
/// use std::io::BufReader;
902907
/// use std::fs::File;
903908
///
904909
/// fn main() -> io::Result<()> {
905-
/// let f = File::open("foo.txt")?;
910+
/// let f = BufReader::new(File::open("foo.txt")?);
906911
///
907912
/// for byte in f.bytes() {
908913
/// println!("{}", byte.unwrap());

0 commit comments

Comments
 (0)