Sink And Empty Discard Endpoints

Point writes at io::sink and reads at io::empty to discard or fabricate EOF, counting bytes moved without storing any of them.

RustCli Tooling

Code

rustsnippet
19 lines
use std::io::{self, Read, Write};

fn main() {
    // discard endpoint: writes to io::sink succeed and the bytes vanish
    let mut sink = io::sink();
    let written = sink.write(b"these bytes go nowhere").unwrap();
    println!("sink accepted {written} bytes");

    // io::empty fabricates EOF: every read reports zero bytes
    let mut empty = io::empty();
    let mut buf = [0u8; 16];
    let got = empty.read(&mut buf).unwrap();
    println!("empty read returned {got} bytes");

    // copy counts the bytes moved without storing any of them
    let payload: &[u8] = b"streamed payload";
    let moved = io::copy(&mut payload, &mut sink).unwrap();
    println!("copy moved {moved} bytes");
}

How it works

Sink And Empty Discard Endpoints keeps everything in the function main.

Putting it to use

The run emits 3 labeled sections — sink accepted, empty read returned, copy moved — one per step.

Core lines

let written = sink.write(b"these bytes go nowhere").unwrap();
let moved = io::copy(&mut payload, &mut sink).unwrap();
let got = empty.read(&mut buf).unwrap();

Interactive Tools for This Topic

FAQ

What does Sink And Empty Discard Endpoints print when run?
Execute the whole file; each printed line is prefixed with a label like `sink accepted`, `empty read returned`, `copy moved`.