Function std::io::stdin_locked[][src]

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

Constructs a new locked handle to the standard input 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::stdin function to obtain an unlocked handle, along with the Stdin::lock method.

The lock is released when the returned guard goes out of scope. The returned guard also implements the Read and BufRead traits for accessing the underlying data.

Note: The mutex locked by this handle is not reentrant. Even in a single-threaded program, calling other code that accesses Stdin could cause a deadlock or panic, if this locked handle is held across that call.

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 read bytes that are not valid UTF-8 will return an error.

Examples

#![feature(stdio_locked)]
use std::io::{self, BufRead};

fn main() -> io::Result<()> {
    let mut buffer = String::new();
    let mut handle = io::stdin_locked();

    handle.read_line(&mut buffer)?;
    Ok(())
}
Run