Skip to content

Commit bfe5a96

Browse files
kiseitai3kiseitai3
authored and
kiseitai3
committed
docs: Small clarification on the usage of read_to_string and read_to_end trait methods.
1 parent ccf3198 commit bfe5a96

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

library/std/src/io/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,17 @@ 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 "Data" | pv | <my_rust_program`).
928+
///
929+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
930+
///
920931
/// [`Vec::try_reserve`]: crate::vec::Vec::try_reserve
921932
#[stable(feature = "rust1", since = "1.0.0")]
922933
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize> {
@@ -960,6 +971,17 @@ pub trait Read {
960971
/// (See also the [`std::fs::read_to_string`] convenience function for
961972
/// reading from a file.)
962973
///
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. FFor example,
979+
/// `cat <file> | <my_rust_program>` will correctly terminate with an `EOF` upon closure of cat.
980+
/// Reading user input or running programs that remain open indefinitely will never terminate
981+
/// the stream with `EOF` (e.g. `yes "Data" | pv | <my_rust_program`).
982+
///
983+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
984+
///
963985
/// [`std::fs::read_to_string`]: crate::fs::read_to_string
964986
#[stable(feature = "rust1", since = "1.0.0")]
965987
fn read_to_string(&mut self, buf: &mut String) -> Result<usize> {
@@ -1262,6 +1284,17 @@ pub trait Read {
12621284
/// Ok(())
12631285
/// }
12641286
/// ```
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+
/// Reading user input or running programs that remain open indefinitely will never terminate
1295+
/// the stream with `EOF` (e.g. `yes "Data" | pv | <my_rust_program`).
1296+
///
1297+
/// Using `.lines()` with a [`BufReader`] or using [`read`] can provide a better solution
12651298
#[stable(feature = "io_read_to_string", since = "1.65.0")]
12661299
pub fn read_to_string<R: Read>(mut reader: R) -> Result<String> {
12671300
let mut buf = String::new();

0 commit comments

Comments
 (0)