diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-08-28 09:35:15 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-28 09:35:15 +0200 |
| commit | 83e83056e7989e0542ea7b5593abf63c7093d6fd (patch) | |
| tree | 76d07049a378405a499e6e707afcb8dccb9aae6c | |
| parent | 58174e3f7cdbab1fedcd268465a01b891e9e62e2 (diff) | |
| parent | 8509936e8f48c64c063b7ba858b881377007f416 (diff) | |
| download | rust-83e83056e7989e0542ea7b5593abf63c7093d6fd.tar.gz rust-83e83056e7989e0542ea7b5593abf63c7093d6fd.zip | |
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
| -rw-r--r-- | library/std/src/io/mod.rs | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 8b8ec32bf5b..a9fa7b0a6ac 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -887,6 +887,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`: @@ -899,10 +903,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()); |
