Skip to content

Commit 8509936

Browse files
authored
Add mention of BufReader in Read::bytes docs
1 parent d70c0ec commit 8509936

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
@@ -883,6 +883,10 @@ pub trait Read {
883883
/// The yielded item is [`Ok`] if a byte was successfully read and [`Err`]
884884
/// otherwise. EOF is mapped to returning [`None`] from this iterator.
885885
///
886+
/// The default implementation calls `read` for each byte,
887+
/// which can be very inefficient for data that's not in memory,
888+
/// such as [`File`]. Consider using a [`BufReader`] in such cases.
889+
///
886890
/// # Examples
887891
///
888892
/// [`File`]s implement `Read`:
@@ -895,10 +899,11 @@ pub trait Read {
895899
/// ```no_run
896900
/// use std::io;
897901
/// use std::io::prelude::*;
902+
/// use std::io::BufReader;
898903
/// use std::fs::File;
899904
///
900905
/// fn main() -> io::Result<()> {
901-
/// let f = File::open("foo.txt")?;
906+
/// let f = BufReader::new(File::open("foo.txt")?);
902907
///
903908
/// for byte in f.bytes() {
904909
/// println!("{}", byte.unwrap());

0 commit comments

Comments
 (0)