A Rust implementation of the Unix wc command-line tool, built as part of the Coding Challenges series.
wcr is a word, line, character, and byte count utility that follows the Unix philosophy of doing one thing well. It can read from files or standard input, making it perfect for use in shell pipelines.
- Count bytes (
-c): Count the number of bytes in the input - Count lines (
-l): Count the number of lines in the input - Count words (
-w): Count the number of words in the input - Count characters (
-m): Count the number of characters in the input - Default mode: When no options are provided, displays lines, words, and bytes
- Standard input support: Reads from stdin when no filename is provided
- Zero external dependencies: Uses only Rust's standard library
- Rust (edition 2024)
git clone git@github.com:MichaelKlank/wcr.git
cd wcr
cargo build --releaseThe binary will be located at target/release/wcr.
cargo install --path .Count lines, words, and bytes (default):
wcr file.txtCount bytes:
wcr -c file.txtCount lines:
wcr -l file.txtCount words:
wcr -w file.txtCount characters:
wcr -m file.txtYou can combine multiple options:
wcr -l -w file.txtRead from stdin:
cat file.txt | wcr -lOr:
echo "Hello World" | wcr# Default output (lines, words, bytes)
$ wcr test.txt
7145 58164 342190 test.txt
# Count lines only
$ wcr -l test.txt
7145 test.txt
# Count words only
$ wcr -w test.txt
58164 test.txt
# Count bytes only
$ wcr -c test.txt
342190 test.txt
# Count characters only
$ wcr -m test.txt
339292 test.txt
# Read from stdin
$ echo "Hello World" | wcr
1 2 12This implementation is written in idiomatic Rust and follows best practices:
- Type safety: Uses
Option<T>instead of magic values - Error handling: Proper error handling with
Result<T, E> - Structured data: Uses structs to organize related data
- Iterator methods: Leverages Rust's iterator API for efficiency
- Zero dependencies: Only uses Rust's standard library
Run the test suite:
cargo testThis project was created as part of the Build Your Own wc Tool coding challenge. The challenge encourages developers to build their own version of Unix command-line tools to understand how they work and to practice good software engineering principles.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Inspired by the Coding Challenges series
- Based on the Unix
wccommand