pub_just::io

Struct Stdin

1.0.0 ยท Source
pub struct Stdin { /* private fields */ }
Expand description

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ยง

Sourceยง

impl Stdin

1.0.0 ยท Source

pub fn lock(&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(())
}
1.0.0 ยท Source

pub fn read_line(&self, buf: &mut String) -> Result<usize, Error>

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
1.62.0 ยท Source

pub fn lines(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());
}

Trait Implementationsยง

1.63.0 ยท Sourceยง

impl AsFd for Stdin

Sourceยง

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

Borrows the file descriptor. Read more
1.21.0 ยท Sourceยง

impl AsRawFd for Stdin

Sourceยง

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
1.16.0 ยท Sourceยง

impl Debug for Stdin

Sourceยง

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

Formats the value using the given formatter. Read more
1.70.0 ยท Sourceยง

impl IsTerminal for Stdin

Sourceยง

fn is_terminal(&self) -> bool

Returns true if the descriptor/handle refers to a terminal/tty. Read more
Sourceยง

impl IsTerminal for Stdin

Sourceยง

fn is_terminal(&self) -> bool

Returns true if the descriptor/handle refers to a terminal/tty. Read more
1.78.0 ยท Sourceยง

impl Read for &Stdin

Sourceยง

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Sourceยง

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

๐Ÿ”ฌThis is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Sourceยง

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Sourceยง

fn is_read_vectored(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Sourceยง

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
Sourceยง

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
Sourceยง

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Sourceยง

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

๐Ÿ”ฌThis is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 ยท Sourceยง

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a โ€œby referenceโ€ adaptor for this instance of Read. Read more
1.0.0 ยท Sourceยง

fn bytes(self) -> Bytes<Self> โ“˜
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 ยท Sourceยง

fn chain<R>(self, next: R) -> Chain<Self, R> โ“˜
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 ยท Sourceยง

fn take(self, limit: u64) -> Take<Self> โ“˜
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
1.0.0 ยท Sourceยง

impl Read for Stdin

Sourceยง

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Sourceยง

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

๐Ÿ”ฌThis is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Sourceยง

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Sourceยง

fn is_read_vectored(&self) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Sourceยง

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
Sourceยง

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
Sourceยง

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Sourceยง

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

๐Ÿ”ฌThis is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 ยท Sourceยง

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a โ€œby referenceโ€ adaptor for this instance of Read. Read more
1.0.0 ยท Sourceยง

fn bytes(self) -> Bytes<Self> โ“˜
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 ยท Sourceยง

fn chain<R>(self, next: R) -> Chain<Self, R> โ“˜
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 ยท Sourceยง

fn take(self, limit: u64) -> Take<Self> โ“˜
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

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

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

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

Sourceยง

impl<T> Pointable for T

Sourceยง

const ALIGN: usize = _

The alignment of pointer.
Sourceยง

type Init = T

The type for initializers.
Sourceยง

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Sourceยง

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Sourceยง

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Sourceยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.
Sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Sourceยง

fn vzip(self) -> V