mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
Auto merge of #87820 - elichai:patch-2, r=kennytm
Replace read_to_string with read_line in Stdin example The current example results in infinitely reading from stdin, which can confuse newcomers trying to read from stdin. (`@razmag` encountered this while learning the language from the docs)
This commit is contained in:
commit
eaf6f46359
@ -216,12 +216,12 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::io::{self, Read};
|
/// use std::io;
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let mut stdin = io::stdin(); // We get `Stdin` here.
|
/// let mut stdin = io::stdin(); // We get `Stdin` here.
|
||||||
/// stdin.read_to_string(&mut buffer)?;
|
/// stdin.read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -244,14 +244,14 @@ pub struct Stdin {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::io::{self, Read};
|
/// use std::io::{self, BufRead};
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let stdin = io::stdin(); // We get `Stdin` here.
|
/// let stdin = io::stdin(); // We get `Stdin` here.
|
||||||
/// {
|
/// {
|
||||||
/// let mut handle = stdin.lock(); // We get `StdinLock` here.
|
/// let mut handle = stdin.lock(); // We get `StdinLock` here.
|
||||||
/// handle.read_to_string(&mut buffer)?;
|
/// handle.read_line(&mut buffer)?;
|
||||||
/// } // `StdinLock` is dropped here.
|
/// } // `StdinLock` is dropped here.
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
@ -277,11 +277,11 @@ pub struct StdinLock<'a> {
|
|||||||
/// Using implicit synchronization:
|
/// Using implicit synchronization:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::io::{self, Read};
|
/// use std::io;
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// io::stdin().read_to_string(&mut buffer)?;
|
/// io::stdin().read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -289,14 +289,14 @@ pub struct StdinLock<'a> {
|
|||||||
/// Using explicit synchronization:
|
/// Using explicit synchronization:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::io::{self, Read};
|
/// use std::io::{self, BufRead};
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let stdin = io::stdin();
|
/// let stdin = io::stdin();
|
||||||
/// let mut handle = stdin.lock();
|
/// let mut handle = stdin.lock();
|
||||||
///
|
///
|
||||||
/// handle.read_to_string(&mut buffer)?;
|
/// handle.read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -337,13 +337,13 @@ pub fn stdin() -> Stdin {
|
|||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// #![feature(stdio_locked)]
|
/// #![feature(stdio_locked)]
|
||||||
/// use std::io::{self, Read};
|
/// use std::io::{self, BufRead};
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let mut handle = io::stdin_locked();
|
/// let mut handle = io::stdin_locked();
|
||||||
///
|
///
|
||||||
/// handle.read_to_string(&mut buffer)?;
|
/// handle.read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -363,14 +363,14 @@ impl Stdin {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use std::io::{self, Read};
|
/// use std::io::{self, BufRead};
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let stdin = io::stdin();
|
/// let stdin = io::stdin();
|
||||||
/// let mut handle = stdin.lock();
|
/// let mut handle = stdin.lock();
|
||||||
///
|
///
|
||||||
/// handle.read_to_string(&mut buffer)?;
|
/// handle.read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
@ -432,13 +432,13 @@ impl Stdin {
|
|||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// #![feature(stdio_locked)]
|
/// #![feature(stdio_locked)]
|
||||||
/// use std::io::{self, Read};
|
/// use std::io::{self, BufRead};
|
||||||
///
|
///
|
||||||
/// fn main() -> io::Result<()> {
|
/// fn main() -> io::Result<()> {
|
||||||
/// let mut buffer = String::new();
|
/// let mut buffer = String::new();
|
||||||
/// let mut handle = io::stdin().into_locked();
|
/// let mut handle = io::stdin().into_locked();
|
||||||
///
|
///
|
||||||
/// handle.read_to_string(&mut buffer)?;
|
/// handle.read_line(&mut buffer)?;
|
||||||
/// Ok(())
|
/// Ok(())
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
Loading…
Reference in New Issue
Block a user