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.
Code
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
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.JSONPath TesterTest JSONPath expressions against JSON documents with dot notation, brackets, wildcards, and recursive descent.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.