Build a `head`-style CLI in Rust: takes one or more file paths (or reads stdin), accepts `-n <N>` to control the number of lines (default 10), and prints them. Ship as a real Cargo project with `cargo fmt`, `cargo clippy -- -D warnings` clean, and a release-mode binary published as a `cargo install`-able artifact.
clap (with the derive feature) for argument parsing — don't roll your own. cargo add clap --features derive.atty::isnt(atty::Stream::Stdin) or check std::io::stdin().is_terminal() (stable since 1.70).BufReader::new(file) then iterate with .lines(). Don't load the whole file with read_to_string — head -n 3 of a 10 GB file should be instant.anyhow::Result<()> from main. The ? operator + anyhow::Context is enough for a CLI of this size.$ ./head-rs -n 3 README.md
# head-rs
A simple head clone in Rust.
Built with clap and anyhow.
$ ./head-rs README.md src/main.rs
==> README.md <==
# head-rs
[... 10 lines ...]
==> src/main.rs <==
use anyhow::Result;
[... 10 lines ...]
$ printf "a\nb\nc\nd\ne\n" | ./head-rs -n 2
a
b
-c <BYTES> flag to print the first N bytes (UTF-8 boundary aware). Test on a multibyte file.--quiet flag that suppresses the ==> file <== header in multi-file mode. Match BSD head semantics.head-rs (or use a unique name). Verify cargo install head-rs works in a clean shell.proptest: any concatenation of ≥N lines, fed to head -n N, returns the first N lines.