Skip to content

Commit 8531086

Browse files
committed
Add performance note to fs::read docs
1 parent 097efa9 commit 8531086

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/libstd/fs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ fn initial_buffer_size(file: &File) -> usize {
231231
/// Read the entire contents of a file into a bytes vector.
232232
///
233233
/// This is a convenience function for using [`File::open`] and [`read_to_end`]
234-
/// with fewer imports and without an intermediate variable.
234+
/// with fewer imports and without an intermediate variable. It pre-allocates a
235+
/// buffer based on the file size when available, so it is generally faster than
236+
/// reading into a vector created with `Vec::new()`.
235237
///
236238
/// [`File::open`]: struct.File.html#method.open
237239
/// [`read_to_end`]: ../io/trait.Read.html#method.read_to_end
@@ -270,7 +272,9 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
270272
/// Read the entire contents of a file into a string.
271273
///
272274
/// This is a convenience function for using [`File::open`] and [`read_to_string`]
273-
/// with fewer imports and without an intermediate variable.
275+
/// with fewer imports and without an intermediate variable. It pre-allocates a
276+
/// buffer based on the file size when available, so it is generally faster than
277+
/// reading into a string created with `String::new()`.
274278
///
275279
/// [`File::open`]: struct.File.html#method.open
276280
/// [`read_to_string`]: ../io/trait.Read.html#method.read_to_string

0 commit comments

Comments
 (0)