From 8509936e8f48c64c063b7ba858b881377007f416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20D=C4=85bek?= Date: Sun, 14 Aug 2022 11:01:04 +0200 Subject: [PATCH] Add mention of `BufReader` in `Read::bytes` docs --- library/std/src/io/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 96addbd1a0558..a16e2431ea9fc 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -883,6 +883,10 @@ pub trait Read { /// The yielded item is [`Ok`] if a byte was successfully read and [`Err`] /// otherwise. EOF is mapped to returning [`None`] from this iterator. /// + /// The default implementation calls `read` for each byte, + /// which can be very inefficient for data that's not in memory, + /// such as [`File`]. Consider using a [`BufReader`] in such cases. + /// /// # Examples /// /// [`File`]s implement `Read`: @@ -895,10 +899,11 @@ pub trait Read { /// ```no_run /// use std::io; /// use std::io::prelude::*; + /// use std::io::BufReader; /// use std::fs::File; /// /// fn main() -> io::Result<()> { - /// let f = File::open("foo.txt")?; + /// let f = BufReader::new(File::open("foo.txt")?); /// /// for byte in f.bytes() { /// println!("{}", byte.unwrap());