Skip to content

Commit eaf6f46

Browse files
committed
Auto merge of rust-lang#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)
2 parents 7b52ad0 + 4763ef2 commit eaf6f46

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

library/std/src/io/stdio.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,12 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
216216
/// # Examples
217217
///
218218
/// ```no_run
219-
/// use std::io::{self, Read};
219+
/// use std::io;
220220
///
221221
/// fn main() -> io::Result<()> {
222222
/// let mut buffer = String::new();
223223
/// let mut stdin = io::stdin(); // We get `Stdin` here.
224-
/// stdin.read_to_string(&mut buffer)?;
224+
/// stdin.read_line(&mut buffer)?;
225225
/// Ok(())
226226
/// }
227227
/// ```
@@ -244,14 +244,14 @@ pub struct Stdin {
244244
/// # Examples
245245
///
246246
/// ```no_run
247-
/// use std::io::{self, Read};
247+
/// use std::io::{self, BufRead};
248248
///
249249
/// fn main() -> io::Result<()> {
250250
/// let mut buffer = String::new();
251251
/// let stdin = io::stdin(); // We get `Stdin` here.
252252
/// {
253253
/// let mut handle = stdin.lock(); // We get `StdinLock` here.
254-
/// handle.read_to_string(&mut buffer)?;
254+
/// handle.read_line(&mut buffer)?;
255255
/// } // `StdinLock` is dropped here.
256256
/// Ok(())
257257
/// }
@@ -277,26 +277,26 @@ pub struct StdinLock<'a> {
277277
/// Using implicit synchronization:
278278
///
279279
/// ```no_run
280-
/// use std::io::{self, Read};
280+
/// use std::io;
281281
///
282282
/// fn main() -> io::Result<()> {
283283
/// let mut buffer = String::new();
284-
/// io::stdin().read_to_string(&mut buffer)?;
284+
/// io::stdin().read_line(&mut buffer)?;
285285
/// Ok(())
286286
/// }
287287
/// ```
288288
///
289289
/// Using explicit synchronization:
290290
///
291291
/// ```no_run
292-
/// use std::io::{self, Read};
292+
/// use std::io::{self, BufRead};
293293
///
294294
/// fn main() -> io::Result<()> {
295295
/// let mut buffer = String::new();
296296
/// let stdin = io::stdin();
297297
/// let mut handle = stdin.lock();
298298
///
299-
/// handle.read_to_string(&mut buffer)?;
299+
/// handle.read_line(&mut buffer)?;
300300
/// Ok(())
301301
/// }
302302
/// ```
@@ -337,13 +337,13 @@ pub fn stdin() -> Stdin {
337337
///
338338
/// ```no_run
339339
/// #![feature(stdio_locked)]
340-
/// use std::io::{self, Read};
340+
/// use std::io::{self, BufRead};
341341
///
342342
/// fn main() -> io::Result<()> {
343343
/// let mut buffer = String::new();
344344
/// let mut handle = io::stdin_locked();
345345
///
346-
/// handle.read_to_string(&mut buffer)?;
346+
/// handle.read_line(&mut buffer)?;
347347
/// Ok(())
348348
/// }
349349
/// ```
@@ -363,14 +363,14 @@ impl Stdin {
363363
/// # Examples
364364
///
365365
/// ```no_run
366-
/// use std::io::{self, Read};
366+
/// use std::io::{self, BufRead};
367367
///
368368
/// fn main() -> io::Result<()> {
369369
/// let mut buffer = String::new();
370370
/// let stdin = io::stdin();
371371
/// let mut handle = stdin.lock();
372372
///
373-
/// handle.read_to_string(&mut buffer)?;
373+
/// handle.read_line(&mut buffer)?;
374374
/// Ok(())
375375
/// }
376376
/// ```
@@ -432,13 +432,13 @@ impl Stdin {
432432
///
433433
/// ```no_run
434434
/// #![feature(stdio_locked)]
435-
/// use std::io::{self, Read};
435+
/// use std::io::{self, BufRead};
436436
///
437437
/// fn main() -> io::Result<()> {
438438
/// let mut buffer = String::new();
439439
/// let mut handle = io::stdin().into_locked();
440440
///
441-
/// handle.read_to_string(&mut buffer)?;
441+
/// handle.read_line(&mut buffer)?;
442442
/// Ok(())
443443
/// }
444444
/// ```

0 commit comments

Comments
 (0)