Summarize an ASCII stream into four disjoint character classes without echoing its content. Work only with supplied input; this does not contact external services or modify input.
Before you start
- Save as recipe.rs and compile using Rust 1.99.0: rustc --edition=2021 recipe.rs -o recipe
- Run recipe without any arguments, including help flags, and supply the complete input through stdin.
- Reads at most 65537 bytes from stdin and rejects input exceeding 65536 bytes. Accepts empty input. Validates strict UTF-8, rejects a leading UTF-8 BOM, and requires ASCII only before writing successful stdout.
- Counts ASCII A-Z and a-z as letters, 0-9 as digits, and only bytes 9, 10, 12, 13 and 32 as whitespace. Every other ASCII byte, including byte 11 and NUL, counts as other.
- Treats stdin as one stream without line splitting, newline normalization or a retained-line count limit. CR and LF each contribute one whitespace byte.
- Success exits 0 and writes exactly one JSON object followed by a newline, with letters, digits, whitespace and other fields, and no stderr.
- Invalid arguments, invalid input and non-interrupted stdin-read failures exit 2 without stdout and attempt to write exactly error: invalid input followed by a newline to stderr. Interrupted reads are retried.
- Uses only the standard library, with no Cargo dependencies, filesystem access, networking, subprocesses or dynamic loading.
Build and check
- Save the example as recipe.rs.
- Compile with Rust 1.99.0 using rustc --edition=2021 recipe.rs -o recipe.
- Prepare UTF-8 input.txt following the documented operation-specific conventions.
- Run ./recipe < input.txt.
- Inspect the JSON result. Exit 2 with exactly error: invalid input followed by a newline on stderr and no stdout identifies rejected input or stdin-read failure. Output-write failures may also exit nonzero, without that diagnostic and potentially after partial stdout; these are outside the input-error guarantee.
Limits to understand
- Uses a fixed 65537-byte stack buffer plus constant-size counters and standard I/O overhead. Processing time is linear in the bytes read.
- Stdin may block while awaiting data or EOF. An input of exactly 65536 bytes requires EOF to establish acceptance; reading a 65537th byte establishes rejection immediately.
- Successful output does not echo input, but the four counts reveal total length and character-class distribution and may be sensitive.
- Memory-allocation and output-write failures are outside the input-error guarantee. Successful-output write or flush errors cause exit 2 and may leave partial stdout. A failing stderr write can prevent delivery of the required diagnostic.
Run it
rustc --edition=2021 recipe.rs -o recipe
./recipe < input.txtExample input
Az09
!?Expected output
{"letters":2,"digits":2,"whitespace":3,"other":2}
