Skip to content

Commit 44da4a0

Browse files
committed
Add doc for Reading from &str and some related cleanup
1 parent b1409af commit 44da4a0

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/libstd/io/mod.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,8 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
419419
///
420420
/// [`File`]s implement `Read`:
421421
///
422-
/// [`read()`]: trait.Read.html#tymethod.read
423-
/// [`std::io`]: ../../std/io/index.html
424-
/// [`File`]: ../fs/struct.File.html
425-
/// [`BufRead`]: trait.BufRead.html
426-
/// [`BufReader`]: struct.BufReader.html
427-
///
428422
/// ```
429-
/// use std::io;
423+
/// # use std::io;
430424
/// use std::io::prelude::*;
431425
/// use std::fs::File;
432426
///
@@ -449,6 +443,32 @@ fn read_to_end<R: Read + ?Sized>(r: &mut R, buf: &mut Vec<u8>) -> Result<usize>
449443
/// # Ok(())
450444
/// # }
451445
/// ```
446+
///
447+
/// Read from `&str` because [`&[u8]`] implements [`Read`]:
448+
///
449+
/// ```
450+
/// # use std::io;
451+
/// use std::io::prelude::*;
452+
///
453+
/// # fn foo() -> io::Result<()> {
454+
/// let mut b = "This string will be read".as_bytes();
455+
/// let mut buffer = [0; 10];
456+
///
457+
/// // read up to 10 bytes
458+
/// b.read(&mut buffer)?;
459+
///
460+
/// // etc... it works exactly as a File does!
461+
/// # Ok(())
462+
/// # }
463+
/// ```
464+
///
465+
/// [`read()`]: trait.Read.html#tymethod.read
466+
/// [`std::io`]: ../../std/io/index.html
467+
/// [`File`]: ../fs/struct.File.html
468+
/// [`BufRead`]: trait.BufRead.html
469+
/// [`BufReader`]: struct.BufReader.html
470+
/// [`&[u8]`]: primitive.slice.html
471+
///
452472
#[stable(feature = "rust1", since = "1.0.0")]
453473
pub trait Read {
454474
/// Pull some bytes from this source into the specified buffer, returning

0 commit comments

Comments
 (0)