@@ -738,7 +738,7 @@ impl<W> fmt::Display for IntoInnerError<W> {
738738/// reducing the number of actual writes to the file.
739739///
740740/// ```no_run
741- /// use std::fs::File;
741+ /// use std::fs::{self, File} ;
742742/// use std::io::prelude::*;
743743/// use std::io::LineWriter;
744744///
@@ -752,17 +752,30 @@ impl<W> fmt::Display for IntoInnerError<W> {
752752/// let file = File::create("poem.txt")?;
753753/// let mut file = LineWriter::new(file);
754754///
755- /// for &byte in road_not_taken.iter() {
756- /// file.write(&[byte]).unwrap();
757- /// }
755+ /// file.write_all(b"I shall be telling this with a sigh")?;
756+ ///
757+ /// // No bytes are written until a newline is encountered (or
758+ /// // the internal buffer is filled).
759+ /// assert_eq!(fs::read_to_string("poem.txt")?, "");
760+ /// file.write_all(b"\n")?;
761+ /// assert_eq!(
762+ /// fs::read_to_string("poem.txt")?,
763+ /// "I shall be telling this with a sigh\n",
764+ /// );
758765///
759- /// // let's check we did the right thing.
760- /// let mut file = File::open("poem.txt")?;
761- /// let mut contents = String::new();
766+ /// // Write the rest of the poem.
767+ /// file.write_all(b"Somewhere ages and ages hence:
768+ /// Two roads diverged in a wood, and I -
769+ /// I took the one less traveled by,
770+ /// And that has made all the difference.")?;
762771///
763- /// file.read_to_string(&mut contents)?;
772+ /// // The last line of the poem doesn't end in a newline, so
773+ /// // we have to flush or drop the `LineWriter` to finish
774+ /// // writing.
775+ /// file.flush()?;
764776///
765- /// assert_eq!(contents.as_bytes(), &road_not_taken[..]);
777+ /// // Confirm the whole poem was written.
778+ /// assert_eq!(fs::read("poem.txt")?, &road_not_taken[..]);
766779/// Ok(())
767780/// }
768781/// ```
@@ -862,7 +875,7 @@ impl<W: Write> LineWriter<W> {
862875 ///
863876 /// The internal buffer is written out before returning the writer.
864877 ///
865- // # Errors
878+ /// # Errors
866879 ///
867880 /// An `Err` will be returned if an error occurs while flushing the buffer.
868881 ///
0 commit comments