Skip to content

Commit f7bc68b

Browse files
committed
use with_capacity in read read_to_string
1 parent eae615d commit f7bc68b

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

library/std/src/fs.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,8 @@ pub struct DirBuilder {
249249
pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
250250
fn inner(path: &Path) -> io::Result<Vec<u8>> {
251251
let mut file = File::open(path)?;
252-
let mut bytes = Vec::new();
253252
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
254-
bytes.reserve(size as usize);
253+
let mut bytes = Vec::with_capacity(size as usize);
255254
io::default_read_to_end(&mut file, &mut bytes)?;
256255
Ok(bytes)
257256
}
@@ -290,9 +289,8 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
290289
pub fn read_to_string<P: AsRef<Path>>(path: P) -> io::Result<String> {
291290
fn inner(path: &Path) -> io::Result<String> {
292291
let mut file = File::open(path)?;
293-
let mut string = String::new();
294292
let size = file.metadata().map(|m| m.len()).unwrap_or(0);
295-
string.reserve(size as usize);
293+
let mut string = String::with_capacity(size as usize);
296294
io::default_read_to_string(&mut file, &mut string)?;
297295
Ok(string)
298296
}

0 commit comments

Comments
 (0)