@@ -574,6 +574,11 @@ impl fmt::Debug for StdinLock<'_> {
574
574
/// output stream. Access is also synchronized via a lock and explicit control
575
575
/// over locking is available via the [`lock`] method.
576
576
///
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
+ ///
577
582
/// Created by the [`io::stdout`] method.
578
583
///
579
584
/// ### Note: Windows Portability Considerations
@@ -589,6 +594,7 @@ impl fmt::Debug for StdinLock<'_> {
589
594
/// standard library or via raw Windows API calls, will fail.
590
595
///
591
596
/// [`lock`]: Stdout::lock
597
+ /// [`flush`]: Write::flush
592
598
/// [`io::stdout`]: stdout
593
599
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
594
600
pub struct Stdout {
@@ -603,6 +609,11 @@ pub struct Stdout {
603
609
/// This handle implements the [`Write`] trait, and is constructed via
604
610
/// the [`Stdout::lock`] method. See its documentation for more.
605
611
///
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
+ ///
606
617
/// ### Note: Windows Portability Considerations
607
618
///
608
619
/// When operating in a console, the Windows implementation of this stream does not support
@@ -614,6 +625,8 @@ pub struct Stdout {
614
625
/// the contained handle will be null. In such cases, the standard library's `Read` and
615
626
/// `Write` will do nothing and silently succeed. All other I/O operations, via the
616
627
/// standard library or via raw Windows API calls, will fail.
628
+ ///
629
+ /// [`flush`]: Write::flush
617
630
#[ must_use = "if unused stdout will immediately unlock" ]
618
631
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
619
632
pub struct StdoutLock < ' a > {
@@ -628,6 +641,11 @@ static STDOUT: OnceLock<ReentrantLock<RefCell<LineWriter<StdoutRaw>>>> = OnceLoc
628
641
/// is synchronized via a mutex. If you need more explicit control over
629
642
/// locking, see the [`Stdout::lock`] method.
630
643
///
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
+ ///
631
649
/// ### Note: Windows Portability Considerations
632
650
///
633
651
/// 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
668
686
/// Ok(())
669
687
/// }
670
688
/// ```
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
671
705
#[ must_use]
672
706
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
673
707
#[ cfg_attr( not( test) , rustc_diagnostic_item = "io_stdout" ) ]
0 commit comments