Skip to content
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

io: update tokio::io::stdout documentation #6674

Merged
merged 1 commit into from
Jul 16, 2024
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
50 changes: 50 additions & 0 deletions tokio/src/io/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,31 @@ cfg_io_std! {
/// Ok(())
/// }
/// ```
///
/// The following is an example of using `stdio` with loop.
///
/// ```
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() {
/// let messages = vec!["hello", " world\n"];
///
/// // When you use `stdio` in a loop, it is recommended to create
/// // a single `stdio` instance outside the loop and call a write
/// // operation against that instance on each loop.
/// //
/// // Repeatedly creating `stdout` instances inside the loop and
/// // writing to that handle could result in mangled output since
/// // each write operation is handled by a different blocking thread.
/// let mut stdout = io::stdout();
///
/// for message in &messages {
/// stdout.write_all(message.as_bytes()).await.unwrap();
/// stdout.flush().await.unwrap();
/// }
/// }
/// ```
#[derive(Debug)]
pub struct Stdout {
std: SplitByUtf8BoundaryIfWindows<Blocking<std::io::Stdout>>,
Expand Down Expand Up @@ -64,6 +89,31 @@ cfg_io_std! {
/// Ok(())
/// }
/// ```
///
/// The following is an example of using `stdio` with loop.
///
/// ```
/// use tokio::io::{self, AsyncWriteExt};
///
/// #[tokio::main]
/// async fn main() {
/// let messages = vec!["hello", " world\n"];
///
/// // When you use `stdio` in a loop, it is recommended to create
/// // a single `stdio` instance outside the loop and call a write
/// // operation against that instance on each loop.
/// //
/// // Repeatedly creating `stdout` instances inside the loop and
/// // writing to that handle could result in mangled output since
/// // each write operation is handled by a different blocking thread.
/// let mut stdout = io::stdout();
///
/// for message in &messages {
/// stdout.write_all(message.as_bytes()).await.unwrap();
/// stdout.flush().await.unwrap();
/// }
/// }
/// ```
pub fn stdout() -> Stdout {
let std = io::stdout();
Stdout {
Expand Down
Loading