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