Function std::io::stdout_locked[][src]

pub fn stdout_locked() -> StdoutLock<'static>
Notable traits for StdoutLock<'_>
impl Write for StdoutLock<'_>
🔬 This is a nightly-only experimental API. (stdio_locked #86845)
Expand description

Constructs a new locked handle to the standard output of the current process.

Each handle returned is a guard granting locked access to a shared global buffer whose access is synchronized via a mutex. If you need more explicit control over locking, for example, in a multi-threaded program, use the io::stdout function to obtain an unlocked handle, along with the Stdout::lock method.

The lock is released when the returned guard goes out of scope. The returned guard also implements the Write trait for writing data.

Note: Windows Portability Consideration

When operating in a console, the Windows implementation of this stream does not support non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return an error.


use std::io::{self, Write};

fn main() -> io::Result<()> {
    let mut handle = io::stdout_locked();

    handle.write_all(b"hello world")?;