Skip to content

Commit

Permalink
Rollup merge of rust-lang#59532 - mbrubeck:docs, r=Centril
Browse files Browse the repository at this point in the history
In doc examples, don't ignore read/write results

Calling `Read::read` or `Write::write` without checking the returned `usize` value is almost always an error.  Example code in the documentation should demonstrate how to use the return value correctly.  Otherwise, people might copy the example code thinking that it is okay to "fire and forget" these methods.
  • Loading branch information
Centril authored Mar 30, 2019
2 parents 183afcd + b6fb3e3 commit 931151f
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
//! let mut buffer = [0; 10];
//!
//! // read up to 10 bytes
//! f.read(&mut buffer)?;
//! let n = f.read(&mut buffer)?;
//!
//! println!("The bytes: {:?}", buffer);
//! println!("The bytes: {:?}", &buffer[..n]);
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -56,9 +56,9 @@
//! f.seek(SeekFrom::End(-10))?;
//!
//! // read up to 10 bytes
//! f.read(&mut buffer)?;
//! let n = f.read(&mut buffer)?;
//!
//! println!("The bytes: {:?}", buffer);
//! println!("The bytes: {:?}", &buffer[..n]);
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -537,7 +537,9 @@ pub trait Read {
/// let mut buffer = [0; 10];
///
/// // read up to 10 bytes
/// f.read(&mut buffer[..])?;
/// let n = f.read(&mut buffer[..])?;
///
/// println!("The bytes: {:?}", &buffer[..n]);
/// Ok(())
/// }
/// ```
Expand Down Expand Up @@ -1062,12 +1064,23 @@ impl Initializer {
/// use std::fs::File;
///
/// fn main() -> std::io::Result<()> {
/// let data = b"some bytes";
///
/// let mut pos = 0;
/// let mut buffer = File::create("foo.txt")?;
///
/// buffer.write(b"some bytes")?;
/// while pos < data.len() {
/// let bytes_written = buffer.write(&data[pos..])?;
/// pos += bytes_written;
/// }
/// Ok(())
/// }
/// ```
///
/// The trait also provides convenience methods like [`write_all`], which calls
/// `write` in a loop until its entire input has been written.
///
/// [`write_all`]: #method.write_all
#[stable(feature = "rust1", since = "1.0.0")]
#[doc(spotlight)]
pub trait Write {
Expand Down

0 comments on commit 931151f

Please sign in to comment.