Skip to content
Tutorials Center
My learning

Topics

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.

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

let mut out = io::stdout().lock();
writeln!(out, "hello")?;

Try it

Rust Generic code over Read and Write

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.

Rust stdin, Cursor, Seek and errors

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

  • stdout is line-buffered when attached to a terminal; print! without a newline may not appear until you call io::stdout().flush().
  • Wrap raw File or TcpStream in BufReader/BufWriter before doing many small operations. A BufWriter flushes when dropped but ignores errors there, so call flush() yourself.
  • Take impl Read or impl Write parameters instead of File so tests can pass &[u8] and Vec<u8>.
  • &mut R implements Read when R does, so you can lend a reader without giving it up.
  • lines() strips \n and \r\n but allocates a String per line; reuse one with read_line in hot loops.
  • Text must be UTF-8 for read_to_string and lines; use read_to_end for arbitrary bytes.
  • write! to io::Write returns io::Result; to fmt::Write it returns fmt::Result.