From a1e1b5c3fb599ea8ea7ec7f785f3cc35c3be9666 Mon Sep 17 00:00:00 2001
From: Andy Russell <arussell123@gmail.com>
Date: Mon, 18 Jun 2018 23:24:29 -0400
Subject: [PATCH 1/2] rework `LineWriter` example

The original example didn't check the return value of `write()`, didn't
flush the writer, and didn't properly demonstrate the buffering.

Fixes #51621.
---
 src/libstd/io/buffered.rs | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs
index ee297d3783e52..ed085d5c0df8e 100644
--- a/src/libstd/io/buffered.rs
+++ b/src/libstd/io/buffered.rs
@@ -738,7 +738,7 @@ impl<W> fmt::Display for IntoInnerError<W> {
 /// reducing the number of actual writes to the file.
 ///
 /// ```no_run
-/// use std::fs::File;
+/// use std::fs::{self, File};
 /// use std::io::prelude::*;
 /// use std::io::LineWriter;
 ///
@@ -752,17 +752,31 @@ impl<W> fmt::Display for IntoInnerError<W> {
 ///     let file = File::create("poem.txt")?;
 ///     let mut file = LineWriter::new(file);
 ///
-///     for &byte in road_not_taken.iter() {
-///        file.write(&[byte]).unwrap();
-///     }
+///     file.write_all(b"I shall be telling this with a sigh")?;
+///
+///     // No bytes are written until a newline is encountered (or
+///     // the internal buffer is filled).
+///     assert_eq!(fs::read_to_string("poem.txt")?.as_bytes(), b"");
+///     file.write_all(b"\n")?;
+///     assert_eq!(
+///         fs::read_to_string("poem.txt")?.as_bytes(),
+///         &b"I shall be telling this with a sigh\n"[..],
+///     );
 ///
-///     // let's check we did the right thing.
-///     let mut file = File::open("poem.txt")?;
-///     let mut contents = String::new();
+///     // Write the rest of the poem.
+///     file.write_all(b"Somewhere ages and ages hence:
+/// Two roads diverged in a wood, and I -
+/// I took the one less traveled by,
+/// And that has made all the difference.")?;
 ///
-///     file.read_to_string(&mut contents)?;
+///     // The last line of the poem doesn't end in a newline, so
+///     // we have to flush or drop the `LineWriter` to finish
+///     // writing.
+///     file.flush()?;
 ///
-///     assert_eq!(contents.as_bytes(), &road_not_taken[..]);
+///     // Confirm the whole poem was written.
+///     let mut poem = fs::read_to_string("poem.txt")?;
+///     assert_eq!(poem.as_bytes(), &road_not_taken[..]);
 ///     Ok(())
 /// }
 /// ```
@@ -862,7 +876,7 @@ impl<W: Write> LineWriter<W> {
     ///
     /// The internal buffer is written out before returning the writer.
     ///
-    // # Errors
+    /// # Errors
     ///
     /// An `Err` will be returned if an error occurs while flushing the buffer.
     ///

From c12a75742485dcc3db7b0f374e78f090c323830d Mon Sep 17 00:00:00 2001
From: Andy Russell <arussell123@gmail.com>
Date: Wed, 11 Jul 2018 14:39:22 -0400
Subject: [PATCH 2/2] simplify assertions

---
 src/libstd/io/buffered.rs | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs
index ed085d5c0df8e..189569683a9cf 100644
--- a/src/libstd/io/buffered.rs
+++ b/src/libstd/io/buffered.rs
@@ -756,11 +756,11 @@ impl<W> fmt::Display for IntoInnerError<W> {
 ///
 ///     // No bytes are written until a newline is encountered (or
 ///     // the internal buffer is filled).
-///     assert_eq!(fs::read_to_string("poem.txt")?.as_bytes(), b"");
+///     assert_eq!(fs::read_to_string("poem.txt")?, "");
 ///     file.write_all(b"\n")?;
 ///     assert_eq!(
-///         fs::read_to_string("poem.txt")?.as_bytes(),
-///         &b"I shall be telling this with a sigh\n"[..],
+///         fs::read_to_string("poem.txt")?,
+///         "I shall be telling this with a sigh\n",
 ///     );
 ///
 ///     // Write the rest of the poem.
@@ -775,8 +775,7 @@ impl<W> fmt::Display for IntoInnerError<W> {
 ///     file.flush()?;
 ///
 ///     // Confirm the whole poem was written.
-///     let mut poem = fs::read_to_string("poem.txt")?;
-///     assert_eq!(poem.as_bytes(), &road_not_taken[..]);
+///     assert_eq!(fs::read("poem.txt")?, &road_not_taken[..]);
 ///     Ok(())
 /// }
 /// ```