Rust Reference ยท Standard Library Modules
The std::io module
Reference for std::io: the Read, Write, BufRead and Seek traits, stdin and stdout, BufReader, BufWriter, Cursor, io::Error and ErrorKind, with examples.
std::io defines how Rust programs move bytes. Its core is four traits: Read (pull bytes out of a source), Write (push bytes into a sink), BufRead (a reader with an internal buffer that can hand out lines) and Seek (move a cursor). Files, sockets, standard input and output, byte slices and Vec<u8> all plug into the same traits, so code written against them works with any of those.
The module also provides the buffering wrappers BufReader and BufWriter, in-memory Cursor, the handles stdin(), stdout() and stderr(), and the error type io::Error with its ErrorKind.
use std::io::{self, Read, Write}; let mut out = io::stdout().lock(); writeln!(out, "hello")?;
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
| Trait | Required method | Useful provided methods |
|---|---|---|
Read |
read(&mut self, buf: &mut [u8]) -> io::Result<usize> |
read_exact, read_to_end, read_to_string, bytes, take, chain |
Write |
write(&mut self, buf: &[u8]) -> io::Result<usize>, flush |
write_all, write_fmt (so write!), by_ref |
BufRead |
fill_buf, consume |
read_line, lines, split, read_until |
Seek |
seek(&mut self, pos: SeekFrom) -> io::Result<u64> |
rewind, stream_position |
| Item | Description |
|---|---|
stdin(), stdout(), stderr() |
Process handles; call .lock() for a held lock and faster repeated access. |
BufReader<R>, BufWriter<W>, LineWriter<W> |
Add buffering to any reader or writer. |
Cursor<T> |
In-memory Read, Write and Seek over Vec<u8>, &[u8] or &mut [u8]. |
io::copy(&mut r, &mut w) |
Copy all bytes; returns the count. |
io::empty(), io::sink(), io::repeat(b) |
Trivial readers and writers. |
io::Error, io::Result<T>, ErrorKind |
Error type; kind() gives NotFound, UnexpectedEof, InvalidData, Interrupted, WouldBlock, ... |
io::read_to_string(reader) |
Read a whole reader into a String. |
Errors and edge cases: read returning Ok(0) means end of input (unless the buffer was empty). write may write fewer bytes than given; use write_all. Interrupted errors are retried by read_exact, write_all and read_to_*.
Usage notes
stdoutis line-buffered when attached to a terminal;print!without a newline may not appear until you callio::stdout().flush().- Wrap raw
FileorTcpStreaminBufReader/BufWriterbefore doing many small operations. ABufWriterflushes when dropped but ignores errors there, so callflush()yourself. - Take
impl Readorimpl Writeparameters instead ofFileso tests can pass&[u8]andVec<u8>. &mut RimplementsReadwhenRdoes, so you can lend a reader without giving it up.lines()strips\nand\r\nbut allocates aStringper line; reuse one withread_linein hot loops.- Text must be UTF-8 for
read_to_stringandlines; useread_to_endfor arbitrary bytes. write!toio::Writereturnsio::Result; tofmt::Writeit returnsfmt::Result.
Related
- std::fs, std::fmt, std::process
- Result, Vec
- Tutorial: Read, Write and BufRead, Reading input