about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-12-24 18:00:45 +0000
committerbors <bors@rust-lang.org>2016-12-24 18:00:45 +0000
commit00e61d41859514c906b8b630ea10ececa4f0c2cd (patch)
treef71652ccf2a46ac4162926a233f25415fbdbeb9c /src/libstd
parentd86cf13316a885dbc78fc33becbc718c6ff3468c (diff)
parenta664466405344d26ee9b834658bc625466889dbe (diff)
downloadrust-00e61d41859514c906b8b630ea10ececa4f0c2cd.tar.gz
rust-00e61d41859514c906b8b630ea10ececa4f0c2cd.zip
Auto merge of #38443 - frewsxcv:file-docs, r=brson
Improve the API examples for `std::fs::File`.

Fixes https://github.com/rust-lang/rust/issues/35875.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/fs.rs44
1 files changed, 38 insertions, 6 deletions
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index 176b5f66fc4..6d4d02caa0b 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -35,21 +35,53 @@ use time::SystemTime;
 ///
 /// # Examples
 ///
+/// Create a new file and write bytes to it:
+///
 /// ```no_run
+/// use std::fs::File;
 /// use std::io::prelude::*;
+///
+/// # fn foo() -> std::io::Result<()> {
+/// let mut file = try!(File::create("foo.txt"));
+/// try!(file.write_all(b"Hello, world!"));
+/// # Ok(())
+/// # }
+/// ```
+///
+/// Read the contents of a file into a `String`:
+///
+/// ```no_run
 /// use std::fs::File;
+/// use std::io::prelude::*;
 ///
 /// # fn foo() -> std::io::Result<()> {
-/// let mut f = try!(File::create("foo.txt"));
-/// try!(f.write_all(b"Hello, world!"));
+/// let mut file = try!(File::open("foo.txt"));
+/// let mut contents = String::new();
+/// try!(file.read_to_string(&mut contents));
+/// assert_eq!(contents, "Hello, world!");
+/// # Ok(())
+/// # }
+/// ```
+///
+/// It can be more efficient to read the contents of a file with a buffered
+/// [`Read`]er. This can be accomplished with [`BufReader<R>`]:
+///
+/// ```no_run
+/// use std::fs::File;
+/// use std::io::BufReader;
+/// use std::io::prelude::*;
 ///
-/// let mut f = try!(File::open("foo.txt"));
-/// let mut s = String::new();
-/// try!(f.read_to_string(&mut s));
-/// assert_eq!(s, "Hello, world!");
+/// # fn foo() -> std::io::Result<()> {
+/// let file = try!(File::open("foo.txt"));
+/// let mut buf_reader = BufReader::new(file);
+/// let mut contents = String::new();
+/// try!(buf_reader.read_to_string(&mut contents));
+/// assert_eq!(contents, "Hello, world!");
 /// # Ok(())
 /// # }
 /// ```
+///
+/// [`BufReader`]: ../io/struct.BufReader.html
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct File {
     inner: fs_imp::File,