Skip to content

Commit c0c0a9f

Browse files
committed
Added documentation for flushing
1 parent 124cc92 commit c0c0a9f

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

library/std/src/io/stdio.rs

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

0 commit comments

Comments
 (0)