Skip to content

Commit d501ac5

Browse files
authored
Unrolled build for #142102
Rollup merge of #142102 - kiseitai3:141714_stdin_read_to_string_docs, r=tgross35 docs: Small clarification on the usage of read_to_string and read_to_end trait methods Small clarification on the usage of read_to_string and read_to_end trait methods. The goal is to make it clear that these trait methods will become locked up if attempting to read to the end of stdin (which is a bit non-sensical unless the other end closes the pipe). Fixes: #141714
2 parents c6a9554 + 7d7fedb commit d501ac5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

library/std/src/io/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,19 @@ pub trait Read {
917917
/// # }
918918
/// ```
919919
///
920+
/// # Usage Notes
921+
///
922+
/// `read_to_end` attempts to read a source until EOF, but many sources are continuous streams
923+
/// that do not send EOF. In these cases, `read_to_end` will block indefinitely. Standard input
924+
/// is one such stream which may be finite if piped, but is typically continuous. For example,
925+
/// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
926+
/// Reading user input or running programs that remain open indefinitely will never terminate
927+
/// the stream with `EOF` (e.g. `yes | my-rust-program`).
928+
///
929+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
930+
///
931+
///[`read`]: Read::read
932+
///
920933
/// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
921934
#[stable(feature = "rust1", since = "1.0.0")]
922935
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
@@ -960,6 +973,19 @@ pub trait Read {
960973
/// (See also the [`std::fs::read_to_string`] convenience function for
961974
/// reading from a file.)
962975
///
976+
/// # Usage Notes
977+
///
978+
/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
979+
/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
980+
/// is one such stream which may be finite if piped, but is typically continuous. For example,
981+
/// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
982+
/// Reading user input or running programs that remain open indefinitely will never terminate
983+
/// the stream with `EOF` (e.g. `yes | my-rust-program`).
984+
///
985+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
986+
///
987+
///[`read`]: Read::read
988+
///
963989
/// [`std::fs::read_to_string`]: crate::fs::read_to_string
964990
#[stable(feature = "rust1", since = "1.0.0")]
965991
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
@@ -1262,6 +1288,20 @@ pub trait Read {
12621288
/// Ok(())
12631289
/// }
12641290
/// ```
1291+
///
1292+
/// # Usage Notes
1293+
///
1294+
/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
1295+
/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
1296+
/// is one such stream which may be finite if piped, but is typically continuous. For example,
1297+
/// `cat file | my-rust-program` will correctly terminate with an `EOF` upon closure of cat.
1298+
/// Reading user input or running programs that remain open indefinitely will never terminate
1299+
/// the stream with `EOF` (e.g. `yes | my-rust-program`).
1300+
///
1301+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
1302+
///
1303+
///[`read`]: Read::read
1304+
///
12651305
#[stable(feature = "io_read_to_string", since = "1.65.0")]
12661306
pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
12671307
let mut buf = String::new();

0 commit comments

Comments
 (0)