Skip to content

Added documentation for flushing per #74348 #136798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions library/std/src/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ impl fmt::Debug for StdinLock<'_> {
/// output stream. Access is also synchronized via a lock and explicit control
/// over locking is available via the [`lock`] method.
///
/// By default, the handle is line-buffered when connected to a terminal, meaning
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
/// output, you can manually call the [`flush`] method. When the handle goes out
/// of scope, the buffer is automatically flushed.
///
/// Created by the [`io::stdout`] method.
///
/// ### Note: Windows Portability Considerations
Expand All @@ -589,6 +594,7 @@ impl fmt::Debug for StdinLock<'_> {
/// standard library or via raw Windows API calls, will fail.
///
/// [`lock`]: Stdout::lock
/// [`flush`]: Write::flush
/// [`io::stdout`]: stdout
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Stdout {
Expand All @@ -603,6 +609,11 @@ pub struct Stdout {
/// This handle implements the [`Write`] trait, and is constructed via
/// the [`Stdout::lock`] method. See its documentation for more.
///
/// By default, the handle is line-buffered when connected to a terminal, meaning
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
/// output, you can manually call the [`flush`] method. When the handle goes out
/// of scope, the buffer is automatically flushed.
///
/// ### Note: Windows Portability Considerations
///
/// When operating in a console, the Windows implementation of this stream does not support
Expand All @@ -614,6 +625,8 @@ pub struct Stdout {
/// the contained handle will be null. In such cases, the standard library's `Read` and
/// `Write` will do nothing and silently succeed. All other I/O operations, via the
/// standard library or via raw Windows API calls, will fail.
///
/// [`flush`]: Write::flush
#[must_use = "if unused stdout will immediately unlock"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct StdoutLock<'a> {
Expand All @@ -628,6 +641,11 @@ static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLoc
/// is synchronized via a mutex. If you need more explicit control over
/// locking, see the [`Stdout::lock`] method.
///
/// By default, the handle is line-buffered when connected to a terminal, meaning
/// it flushes automatically when a newline (`\n`) is encountered. For immediate
/// output, you can manually call the [`flush`] method. When the handle goes out
/// of scope, the buffer is automatically flushed.
///
/// ### Note: Windows Portability Considerations
///
/// When operating in a console, the Windows implementation of this stream does not support
Expand Down Expand Up @@ -668,6 +686,22 @@ static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLoc
/// Ok(())
/// }
/// ```
///
/// Ensuring output is flushed immediately:
///
/// ```no_run
/// use std::io::{self, Write};
///
/// fn main() -> io::Result<()> {
/// let mut stdout = io::stdout();
/// stdout.write_all(b"hello, ")?;
/// stdout.flush()?; // Manual flush
/// stdout.write_all(b"world!\n")?; // Automatically flushed
/// Ok(())
/// }
/// ```
///
/// [`flush`]: Write::flush
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "io_stdout")]
Expand Down
Loading