Skip to content

Commit 49bcdfd

Browse files
authored
Rollup merge of rust-lang#80217 - camelid:io-read_to_string, r=m-ou-se
Add a `std::io::read_to_string` function I recognize that you're usually supposed to open an issue first, but the implementation is very small so it's okay if this is closed and it was 'wasted work' :) ----- The equivalent of `std::fs::read_to_string`, but generalized to all `Read` impls. As the documentation on `std::io::read_to_string` says, the advantage of this function is that it means you don't have to create a variable first and it provides more type safety since you can only get the buffer out if there were no errors. If you use `Read::read_to_string`, you have to remember to check whether the read succeeded because otherwise your buffer will be empty. It's friendlier to newcomers and better in most cases to use an explicit return value instead of an out parameter.
2 parents 15a3bab + 7463292 commit 49bcdfd

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

library/std/src/io/mod.rs

+48
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,54 @@ pub trait Read {
944944
}
945945
}
946946

947+
/// Read all bytes from a [reader][Read] into a new [`String`].
948+
///
949+
/// This is a convenience function for [`Read::read_to_string`]. Using this
950+
/// function avoids having to create a variable first and provides more type
951+
/// safety since you can only get the buffer out if there were no errors. (If you
952+
/// use [`Read::read_to_string`] you have to remember to check whether the read
953+
/// succeeded because otherwise your buffer will be empty or only partially full.)
954+
///
955+
/// # Performance
956+
///
957+
/// The downside of this function's increased ease of use and type safety is
958+
/// that it gives you less control over performance. For example, you can't
959+
/// pre-allocate memory like you can using [`String::with_capacity`] and
960+
/// [`Read::read_to_string`]. Also, you can't re-use the buffer if an error
961+
/// occurs while reading.
962+
///
963+
/// In many cases, this function's performance will be adequate and the ease of use
964+
/// and type safety tradeoffs will be worth it. However, there are cases where you
965+
/// need more control over performance, and in those cases you should definitely use
966+
/// [`Read::read_to_string`] directly.
967+
///
968+
/// # Errors
969+
///
970+
/// This function forces you to handle errors because the output (the `String`)
971+
/// is wrapped in a [`Result`]. See [`Read::read_to_string`] for the errors
972+
/// that can occur. If any error occurs, you will get an [`Err`], so you
973+
/// don't have to worry about your buffer being empty or partially full.
974+
///
975+
/// # Examples
976+
///
977+
/// ```no_run
978+
/// #![feature(io_read_to_string)]
979+
///
980+
/// # use std::io;
981+
/// fn main() -> io::Result<()> {
982+
/// let stdin = io::read_to_string(&mut io::stdin())?;
983+
/// println!("Stdin was:");
984+
/// println!("{}", stdin);
985+
/// Ok(())
986+
/// }
987+
/// ```
988+
#[unstable(feature = "io_read_to_string", issue = "80218")]
989+
pub fn read_to_string<R: Read>(reader: &mut R) -> Result<String> {
990+
let mut buf = String::new();
991+
reader.read_to_string(&mut buf)?;
992+
Ok(buf)
993+
}
994+
947995
/// A buffer type used with `Read::read_vectored`.
948996
///
949997
/// It is semantically a wrapper around an `&mut [u8]`, but is guaranteed to be

0 commit comments

Comments
 (0)