Skip to content

Commit 2053ad7

Browse files
authored
Rollup merge of rust-lang#136798 - pcorwin:master, r=tgross35
Added documentation for flushing per rust-lang#74348 Resolves rust-lang#74348
2 parents f21f527 + 9119bf3 commit 2053ad7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

std/src/io/stdio.rs

+34
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,11 @@ impl fmt::Debug for StdinLock<'_> {
575575
/// output stream. Access is also synchronized via a lock and explicit control
576576
/// over locking is available via the [`lock`] method.
577577
///
578+
/// By default, the handle is line-buffered when connected to a terminal, meaning
579+
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
580+
/// output, you can manually call the [`flush`] method. When the handle goes out
581+
/// of scope, the buffer is automatically flushed.
582+
///
578583
/// Created by the [`io::stdout`] method.
579584
///
580585
/// ### Note: Windows Portability Considerations
@@ -590,6 +595,7 @@ impl fmt::Debug for StdinLock<'_> {
590595
/// standard library or via raw Windows API calls, will fail.
591596
///
592597
/// [`lock`]: Stdout::lock
598+
/// [`flush`]: Write::flush
593599
/// [`io::stdout`]: stdout
594600
#[stable(feature = "rust1", since = "1.0.0")]
595601
pub struct Stdout {
@@ -604,6 +610,11 @@ pub struct Stdout {
604610
/// This handle implements the [`Write`] trait, and is constructed via
605611
/// the [`Stdout::lock`] method. See its documentation for more.
606612
///
613+
/// By default, the handle is line-buffered when connected to a terminal, meaning
614+
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
615+
/// output, you can manually call the [`flush`] method. When the handle goes out
616+
/// of scope, the buffer is automatically flushed.
617+
///
607618
/// ### Note: Windows Portability Considerations
608619
///
609620
/// When operating in a console, the Windows implementation of this stream does not support
@@ -615,6 +626,8 @@ pub struct Stdout {
615626
/// the contained handle will be null. In such cases, the standard library's `Read` and
616627
/// `Write` will do nothing and silently succeed. All other I/O operations, via the
617628
/// standard library or via raw Windows API calls, will fail.
629+
///
630+
/// [`flush`]: Write::flush
618631
#[must_use = "if unused stdout will immediately unlock"]
619632
#[stable(feature = "rust1", since = "1.0.0")]
620633
pub struct StdoutLock<'a> {
@@ -629,6 +642,11 @@ static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLoc
629642
/// is synchronized via a mutex. If you need more explicit control over
630643
/// locking, see the [`Stdout::lock`] method.
631644
///
645+
/// By default, the handle is line-buffered when connected to a terminal, meaning
646+
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
647+
/// output, you can manually call the [`flush`] method. When the handle goes out
648+
/// of scope, the buffer is automatically flushed.
649+
///
632650
/// ### Note: Windows Portability Considerations
633651
///
634652
/// When operating in a console, the Windows implementation of this stream does not support
@@ -669,6 +687,22 @@ static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLoc
669687
/// Ok(())
670688
/// }
671689
/// ```
690+
///
691+
/// Ensuring output is flushed immediately:
692+
///
693+
/// ```no_run
694+
/// use std::io::{self, Write};
695+
///
696+
/// fn main() -> io::Result<()> {
697+
/// let mut stdout = io::stdout();
698+
/// stdout.write_all(b"hello, ")?;
699+
/// stdout.flush()?; // Manual flush
700+
/// stdout.write_all(b"world!\n")?; // Automatically flushed
701+
/// Ok(())
702+
/// }
703+
/// ```
704+
///
705+
/// [`flush`]: Write::flush
672706
#[must_use]
673707
#[stable(feature = "rust1", since = "1.0.0")]
674708
#[cfg_attr(not(test), rustc_diagnostic_item = "io_stdout")]

0 commit comments

Comments
 (0)