From 0f24dd22a701a03cea1fda8b91ecebc8e1179538 Mon Sep 17 00:00:00 2001 From: Ian Rettig Date: Wed, 11 Jun 2025 11:25:34 -0400 Subject: [PATCH] Feat: Add word counter binary. --- src/bin/counter.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/bin/counter.rs diff --git a/src/bin/counter.rs b/src/bin/counter.rs new file mode 100644 index 0000000..05a314b --- /dev/null +++ b/src/bin/counter.rs @@ -0,0 +1,22 @@ +use clap::Parser; +use std::path::PathBuf; + +/// Count the number of lines in a file +#[derive(Parser)] +#[command(arg_required_else_help = true)] +struct Cli { + /// The path to the file to read + file: PathBuf, +} + +fn main() { + let args = Cli::parse(); + let mut word_count = 0; + let file = args.file; + + for line in std::fs::read_to_string(&file).unwrap().lines() { + word_count += line.split(' ').count(); + } + + println!("Words in {}: {}", file.to_str().unwrap(), word_count) +}