about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2023-01-11 00:19:27 +0800
committeryukang <moorekang@gmail.com>2023-01-11 00:19:27 +0800
commitf7bc68bb4edd1974f7c4aa6113902132429c00e8 (patch)
treeec9db058c6d1309684c61d5159d8b6fa977cf717
parenteae615dfdd9222a49182e59cc15bd47da1d536b6 (diff)
downloadrust-f7bc68bb4edd1974f7c4aa6113902132429c00e8.tar.gz
rust-f7bc68bb4edd1974f7c4aa6113902132429c00e8.zip
use with_capacity in read read_to_string
-rw-r--r--library/std/src/fs.rs6
1 files changed, 2 insertions, 4 deletions
diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs
index 6ddd5c28cc2..5c5ef0b1125 100644
--- a/library/std/src/fs.rs
+++ b/library/std/src/fs.rs
@@ -249,9 +249,8 @@ pub struct DirBuilder {
 pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
     fn inner(path: &Path) -> io::Result<Vec<u8>> {
         let mut file = File::open(path)?;
-        let mut bytes = Vec::new();
         let size = file.metadata().map(|m| m.len()).unwrap_or(0);
-        bytes.reserve(size as usize);
+        let mut bytes = Vec::with_capacity(size as usize);
         io::default_read_to_end(&mut file, &mut bytes)?;
         Ok(bytes)
     }
@@ -290,9 +289,8 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
 pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
     fn inner(path: &Path) -> io::Result<String> {
         let mut file = File::open(path)?;
-        let mut string = String::new();
         let size = file.metadata().map(|m| m.len()).unwrap_or(0);
-        string.reserve(size as usize);
+        let mut string = String::with_capacity(size as usize);
         io::default_read_to_string(&mut file, &mut string)?;
         Ok(string)
     }