Standard Input and Output
std.io
std::io is the Rust standard library module that provides core traits, buffered wrappers, standard stream handles, in-memory cursors, and error types for byte-oriented and UTF-8 input and output across files, network sockets, pipes, and memory buffers.
Architecture of std::io
+-------------------------------------------------------------------+
| Core `std::io` Trait Hierarchy |
| |
| Read (Raw byte input) Write (Raw & formatted output)|
| ^ ^ |
| | (Buffered extension) | |
| BufRead (`read_line`, `lines`) Seek (`seek`, `stream_position`)|
+-----------------+-------------------+-----------------------------+
|
v
+-------------------------------------------------------------------+
| Concrete Implementors & Adapter Wrappers |
| - Standard Streams: `Stdin`, `Stdout`, `Stderr` (+ `*Lock`) |
| - Buffered Wrappers: `BufReader<R>`, `BufWriter<W>`, `LineWriter`|
| - In-Memory Buffers: `&[u8]` (`Read`), `Vec<u8>` (`Write`), `Cursor<T>`|
| - OS Handles: `std::fs::File`, `std::net::TcpStream`, `ChildStdin`|
+-------------------------------------------------------------------+
Summary of Key Types, Traits, and Functions in std::io
| Category | Items in std::io |
Purpose |
|---|---|---|
| Core Traits | Read, BufRead, Write, Seek, IsTerminal |
Unified byte-stream and cursor positioning interfaces |
| Standard Streams | stdin(), stdout(), stderr(), StdinLock, StdoutLock, StderrLock |
Process-wide file descriptors 0, 1, and 2 |
| Buffered Wrappers | BufReader<R>, BufWriter<W>, LineWriter<W> |
Reduces OS system-call overhead with an in-memory buffer (8 KiB default) |
| In-Memory & Utility Streams | Cursor<T>, Empty (empty()), Repeat (repeat(b)), Sink (sink()) |
Zero-syscall in-memory readers, writers, and /dev/null equivalents |
| Error Handling | io::Error, io::ErrorKind, io::Result<T> |
Unified OS errno / Win32 error wrapper and Result<T, io::Error> alias |
| Stream Copying | io::copy(&mut reader, &mut writer), io::read_to_string(reader) |
High-performance bulk transfer between any Read and Write |
The std::io::prelude Module
Importing use std::io::prelude::*; brings the four foundational I/O traits (Read, BufRead, Write, Seek) into scope at once so their methods (.read_to_string(), .lines(), .write_all(), .flush()) are available on all I/O types:
use std::io::{self, Cursor, prelude::*};
fn main() -> io::Result<()> {
// `Cursor<Vec<u8>>` implements `Read`, `BufRead`, `Write`, and `Seek` in RAM:
let mut buffer = Cursor::new(Vec::new());
// 1. `Write` trait methods:
buffer.write_all(b"line one\nline two\n")?;
// 2. `Seek` trait method (rewind back to byte offset 0):
buffer.rewind()?;
// 3. `BufRead` trait method (`.lines()` iterator):
for (idx, line) in buffer.lines().enumerate() {
println!("line #{idx}: {:?}", line?);
}
// 4. `io::copy` into `io::sink()` (in-memory equivalent of `/dev/null`):
let mut source: &[u8] = b"discarded bytes";
let copied = io::copy(&mut source, &mut io::sink())?;
println!("copied {} bytes into io::sink()", copied);
Ok(())
}
line #0: "line one"
line #1: "line two"
copied 15 bytes into io::sink()