A simple command-line text search utility written in Rust that searches for a specified query within text files.
minigrep is a lightweight version of the classic grep command-line tool. It takes a search query and a file path as arguments, then prints all lines from the file that contain the specified query string.
- Simple text search: Find all lines in a file containing a specific query
- Error handling: Robust error handling for file operations and invalid arguments
- Unit tests: Includes test coverage for core functionality
- Clean architecture: Separation of concerns with library code in
src/lib.rsand CLI interface insrc/main.rs
- Rust (2024 edition or later)
-
Clone the repository:
git clone <repository-url> cd minigrep
-
Build the project:
cargo build
-
For a release build:
cargo build --release
Run the program with a search query and a file path:
cargo run <query> <file_path>Search for the word "nobody" in the included poem.txt:
cargo run nobody poem.txtSearch for "frog" in the poem:
cargo run frog poem.txtWhen searching for "nobody" in poem.txt:
I'm nobody! Who are you?
Are you nobody, too?
minigrep/
├── src/
│ ├── lib.rs # Core library with search functionality and Config struct
│ └── main.rs # CLI interface and argument parsing
├── Cargo.toml # Project configuration and dependencies
├── poem.txt # Sample text file for testing
└── README.md # This file
Configstruct: Handles command-line argument parsing and validationrunfunction: Main application logic that reads files and orchestrates the searchsearchfunction: Core search algorithm that finds matching lines- Error handling: Uses
Resulttypes and proper error propagation throughout
Run the included unit tests:
cargo testThe project includes tests for the core search functionality in src/lib.rs:47-61.
The program handles several error conditions gracefully:
- Insufficient arguments: Displays usage information when fewer than 2 arguments are provided
- File not found: Shows an appropriate error message if the specified file doesn't exist
- File read errors: Handles I/O errors that may occur during file reading
This project follows Rust best practices:
- Uses the 2024 Rust edition
- Implements proper error handling with
Resulttypes - Separates library code from binary code
- Includes unit tests for core functionality
- Uses idiomatic Rust patterns like iterators and string slices
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass with
cargo test - Submit a pull request
This project is a learning exercise based on the Rust programming language tutorial.