Skip to content

Commit bc1b43c

Browse files
committed
doc: add example for Stdin::read_line
1 parent 912ab64 commit bc1b43c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/libstd/io/stdio.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ impl Stdin {
204204
///
205205
/// For detailed semantics of this method, see the documentation on
206206
/// `BufRead::read_line`.
207+
///
208+
/// # Examples
209+
///
210+
/// ```no_run
211+
/// use std::io;
212+
///
213+
/// let mut input = String::new();
214+
/// match io::stdin().read_line(&mut input) {
215+
/// Ok(n) => {
216+
/// println!("{} bytes read", n);
217+
/// println!("{}", input);
218+
/// }
219+
/// Err(error) => println!("error: {}", error),
220+
/// }
221+
/// ```
222+
///
223+
/// You can run the example one of two ways:
224+
///
225+
/// - Pipe some text to it, e.g. `printf foo | path/to/executable`
226+
/// - Give it text interactively by running the executable directly,
227+
// in which case it will wait for the Enter key to be pressed before
228+
/// continuing
207229
#[stable(feature = "rust1", since = "1.0.0")]
208230
pub fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
209231
self.lock().read_line(buf)

0 commit comments

Comments
 (0)