Skip to content

Commit ffabd27

Browse files
author
kiseitai3
committed
docs: Amended usage notes sections in read_to_end/read_to_string. Added usage note section to std::io::read_to_string as well.
1 parent 95ff1e4 commit ffabd27

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

library/std/src/io/mod.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,16 @@ pub trait Read {
917917
/// # }
918918
/// ```
919919
///
920-
/// # Notes
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+
/// `yes "Data" | pv | <my_rust_program` or `tail -f <file> | <my_rust_program>` may not close
927+
/// the stream or insert an `EOF` termination character.
921928
///
922-
/// Be careful using this trait method with streams that are expected to be continuous. For example, using
923-
/// `read_to_end` with streams like `stdin` will simply lock the application into waiting on the
924-
/// transmission of data to conclude. It is recommended you use this method if you know the stream will be closed
925-
/// at the other end. The problem is that EOF or End of File is never reached for streams that never close or are finite.
929+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
926930
///
927931
/// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
928932
#[stable(feature = "rust1", since = "1.0.0")]
@@ -964,16 +968,20 @@ pub trait Read {
964968
/// }
965969
/// ```
966970
///
967-
/// # Notes
968-
///
969-
/// Be careful using this trait method with streams that are expected to be continuous. For example, using
970-
/// `read_to_string` with streams like `stdin` will simply lock the application into waiting on the
971-
/// transmission of data to conclude. It is recommended you use this method if you know the stream will be closed
972-
/// at the other end. The problem is that EOF or End of File is never reached for streams that never close or are finite.
973-
///
974971
/// (See also the [`std::fs::read_to_string`] convenience function for
975972
/// reading from a file.)
976973
///
974+
/// # Usage Notes
975+
///
976+
/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
977+
/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
978+
/// is one such stream which may be finite if piped, but is typically continuous. For example,
979+
/// `cat <file> | <my_rust_program>` will correctly terminate with an `EOF` upon closure of cat.
980+
/// `yes "Data" | pv | <my_rust_program` or `tail -f <file> | <my_rust_program>` may not close
981+
/// the stream or insert an `EOF` termination character.
982+
///
983+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
984+
///
977985
/// [`std::fs::read_to_string`]: crate::fs::read_to_string
978986
#[stable(feature = "rust1", since = "1.0.0")]
979987
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
@@ -1276,6 +1284,17 @@ pub trait Read {
12761284
/// Ok(())
12771285
/// }
12781286
/// ```
1287+
///
1288+
/// # Usage Notes
1289+
///
1290+
/// `read_to_string` attempts to read a source until EOF, but many sources are continuous streams
1291+
/// that do not send EOF. In these cases, `read_to_string` will block indefinitely. Standard input
1292+
/// is one such stream which may be finite if piped, but is typically continuous. For example,
1293+
/// `cat <file> | <my_rust_program>` will correctly terminate with an `EOF` upon closure of cat.
1294+
/// `yes "Data" | pv | <my_rust_program` or `tail -f <file> | <my_rust_program>` may not close
1295+
/// the stream or insert an `EOF` termination character.
1296+
///
1297+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
12791298
#[stable(feature = "io_read_to_string", since = "1.65.0")]
12801299
pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
12811300
let mut buf = String::new();

0 commit comments

Comments
 (0)