Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

minigrep

A simple command-line text search utility written in Rust that searches for a specified query within text files.

Overview

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.

Features

  • 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.rs and CLI interface in src/main.rs

Installation

Prerequisites

  • Rust (2024 edition or later)

Building from source

  1. Clone the repository:

    git clone <repository-url>
    cd minigrep
  2. Build the project:

    cargo build
  3. For a release build:

    cargo build --release

Usage

Run the program with a search query and a file path:

cargo run <query> <file_path>

Examples

Search for the word "nobody" in the included poem.txt:

cargo run nobody poem.txt

Search for "frog" in the poem:

cargo run frog poem.txt

Expected Output

When searching for "nobody" in poem.txt:

I'm nobody! Who are you?
Are you nobody, too?

Project Structure

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

Code Architecture

  • Config struct: Handles command-line argument parsing and validation
  • run function: Main application logic that reads files and orchestrates the search
  • search function: Core search algorithm that finds matching lines
  • Error handling: Uses Result types and proper error propagation throughout

Testing

Run the included unit tests:

cargo test

The project includes tests for the core search functionality in src/lib.rs:47-61.

Error Handling

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

Development

This project follows Rust best practices:

  • Uses the 2024 Rust edition
  • Implements proper error handling with Result types
  • Separates library code from binary code
  • Includes unit tests for core functionality
  • Uses idiomatic Rust patterns like iterators and string slices

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass with cargo test
  6. Submit a pull request

License

This project is a learning exercise based on the Rust programming language tutorial.

About

A tiny grep implementation in Rust

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages