Struct Stdin

struct Stdin { ... }

A handle to the standard input stream of a process.

Each handle is a shared reference to a global buffer of input data to this process. A handle can be lock'd to gain full access to BufRead methods (e.g., .lines()). Reads to this handle are otherwise locked with respect to other reads.

This handle implements the Read trait, but beware that concurrent reads of Stdin must be executed with care.

Created by the io::stdin method.

Note: Windows Portability Considerations

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.

In a process with a detached console, such as one using #![windows_subsystem = "windows"], or in a child process spawned from such a process, the contained handle will be null. In such cases, the standard library's Read and Write will do nothing and silently succeed. All other I/O operations, via the standard library or via raw Windows API calls, will fail.

Examples

use std::io;

fn main() -> io::Result<()> {
    let mut buffer = String::new();
    let stdin = io::stdin(); // We get `Stdin` here.
    stdin.read_line(&mut buffer)?;
    Ok(())
}

Implementations

impl Stdin

fn lock(self: &Self) -> StdinLock<'static>

Locks this handle to the standard input stream, returning a readable guard.

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

Examples

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

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

    handle.read_line(&mut buffer)?;
    Ok(())
}
fn read_line(self: &Self, buf: &mut String) -> Result<usize>

Locks this handle and reads a line of input, appending it to the specified buffer.

For detailed semantics of this method, see the documentation on BufRead::read_line. In particular:

  • Previous content of the buffer will be preserved. To avoid appending to the buffer, you need to clear it first.
  • The trailing newline character, if any, is included in the buffer.

Examples

use std::io;

let mut input = String::new();
match io::stdin().read_line(&mut input) {
    Ok(n) => {
        println!("{n} bytes read");
        println!("{input}");
    }
    Err(error) => println!("error: {error}"),
}

You can run the example one of two ways:

  • Pipe some text to it, e.g., printf foo | path/to/executable
  • Give it text interactively by running the executable directly, in which case it will wait for the Enter key to be pressed before continuing
fn lines(self: Self) -> Lines<StdinLock<'static>>

Consumes this handle and returns an iterator over input lines.

For detailed semantics of this method, see the documentation on BufRead::lines.

Examples

use std::io;

let lines = io::stdin().lines();
for line in lines {
    println!("got a line: {}", line.unwrap());
}

impl AsFd for Stdin

fn as_fd(self: &Self) -> BorrowedFd<'_>

impl AsHandle for Stdin

fn as_handle(self: &Self) -> BorrowedHandle<'_>

impl AsRawFd for Stdin

fn as_raw_fd(self: &Self) -> RawFd

impl AsRawHandle for Stdin

fn as_raw_handle(self: &Self) -> RawHandle

impl Debug for Stdin

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Freeze for Stdin

impl IsTerminal for Stdin

fn is_terminal(self: &Self) -> bool

impl Read for Stdin

fn read(self: &mut Self, buf: &mut [u8]) -> Result<usize>
fn read_buf(self: &mut Self, buf: BorrowedCursor<'_>) -> Result<()>
fn read_vectored(self: &mut Self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn is_read_vectored(self: &Self) -> bool
fn read_to_end(self: &mut Self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_string(self: &mut Self, buf: &mut String) -> Result<usize>
fn read_exact(self: &mut Self, buf: &mut [u8]) -> Result<()>
fn read_buf_exact(self: &mut Self, cursor: BorrowedCursor<'_>) -> Result<()>

impl RefUnwindSafe for Stdin

impl Send for Stdin

impl StdioExt for Stdin

fn set_fd<T: Into<OwnedFd>>(self: &mut Self, fd: T) -> Result<()>
fn take_fd(self: &mut Self) -> Result<OwnedFd>
fn replace_fd<T: Into<OwnedFd>>(self: &mut Self, replace_with: T) -> Result<OwnedFd>

impl Sync for Stdin

impl Unpin for Stdin

impl UnsafeUnpin for Stdin

impl UnwindSafe for Stdin

impl<T> Any for Stdin

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Stdin

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Stdin

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for Stdin

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Stdin

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Stdin

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Stdin

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>