about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorAndy Russell <arussell123@gmail.com>2018-11-16 16:22:06 -0500
committerAndy Russell <arussell123@gmail.com>2018-12-07 12:54:11 -0500
commit2f6226518bd5085896a0f27cfd3ea396367ecd50 (patch)
tree713f018b5a2028f682ac9fcf84688022e5f6acfe /src/libcore
parent1c3236afc3560b5781acf942a4746aeb11e5374e (diff)
downloadrust-2f6226518bd5085896a0f27cfd3ea396367ecd50.tar.gz
rust-2f6226518bd5085896a0f27cfd3ea396367ecd50.zip
use top level `fs` functions where appropriate
This commit replaces many usages of `File::open` and reading or writing
with `fs::read_to_string`, `fs::read` and `fs::write`. This reduces code
complexity, and will improve performance for most reads, since the
functions allocate the buffer to be the size of the file.

I believe that this commit will not impact behavior in any way, so some
matches will check the error kind in case the file was not valid UTF-8.
Some of these cases may not actually care about the error.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/convert.rs7
1 files changed, 3 insertions, 4 deletions
diff --git a/src/libcore/convert.rs b/src/libcore/convert.rs
index dbc28ef7cf6..2d4813718f4 100644
--- a/src/libcore/convert.rs
+++ b/src/libcore/convert.rs
@@ -327,7 +327,8 @@ pub trait Into<T>: Sized {
 /// An example usage for error handling:
 ///
 /// ```
-/// use std::io::{self, Read};
+/// use std::fs;
+/// use std::io;
 /// use std::num;
 ///
 /// enum CliError {
@@ -348,9 +349,7 @@ pub trait Into<T>: Sized {
 /// }
 ///
 /// fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
-///     let mut file = std::fs::File::open("test")?;
-///     let mut contents = String::new();
-///     file.read_to_string(&mut contents)?;
+///     let mut contents = fs::read_to_string(&file_name)?;
 ///     let num: i32 = contents.trim().parse()?;
 ///     Ok(num)
 /// }