io: Explain how to flush stdout/stderr (#7904)

This commit is contained in:
Stepan Koltsov
2026-02-12 14:06:48 +00:00
committed by GitHub
parent 5bcd7c1d09
commit 9e7e1ef7ad
2 changed files with 29 additions and 1 deletions
+15 -1
View File
@@ -29,7 +29,7 @@ cfg_io_std! {
///
/// #[tokio::main]
/// async fn main() -> io::Result<()> {
/// let mut stderr = io::stdout();
/// let mut stderr = io::stderr();
/// stderr.write_all(b"Print some error here.").await?;
/// Ok(())
/// }
@@ -50,6 +50,20 @@ cfg_io_std! {
/// to occur as a single write, so multiple threads writing data with
/// [`write_all`] may result in interleaved output.
///
/// Note that unlike [`std::io::stderr`], each call to this `stderr()`
/// produces a new writer, so for example, this program does **not** flush stderr:
///
/// ```no_run
/// # use tokio::io::AsyncWriteExt;
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// tokio::io::stderr().write_all(b"aa").await?;
/// tokio::io::stderr().flush().await?;
/// # Ok(())
/// # }
/// ```
///
/// [`std::io::stderr`]: std::io::stderr
/// [`AsyncWrite`]: AsyncWrite
/// [`write_all`]: crate::io::AsyncWriteExt::write_all()
///
+14
View File
@@ -74,6 +74,20 @@ cfg_io_std! {
/// to occur as a single write, so multiple threads writing data with
/// [`write_all`] may result in interleaved output.
///
/// Note that unlike [`std::io::stdout`], each call to this `stdout()`
/// produces a new writer, so for example, this program does **not** flush stdout:
///
/// ```no_run
/// # use tokio::io::AsyncWriteExt;
/// # #[tokio::main]
/// # async fn main() -> std::io::Result<()> {
/// tokio::io::stdout().write_all(b"aa").await?;
/// tokio::io::stdout().flush().await?;
/// # Ok(())
/// # }
/// ```
///
/// [`std::io::stdout`]: std::io::stdout
/// [`AsyncWrite`]: AsyncWrite
/// [`write_all`]: crate::io::AsyncWriteExt::write_all()
///