Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-minigrep

A small, idiomatic Rust implementation of a simplified grep-style CLI and library.

This repository contains a tiny command-line program (and library) that searches for a query string inside a file and prints matching lines. It demonstrates basic Rust CLI patterns, error handling, and unit testing.

Table of contents

Features

  • Simple, dependency-free Rust implementation
  • Case-sensitive and case-insensitive search modes
  • Exposes search functions in the minigrep crate for reuse in other Rust code

Quick start

Prerequisites:

Build and run from the repository root:

PowerShell (Windows):

# Build in debug mode
cargo build

# Run the CLI: cargo will pass remaining arguments to the program
cargo run -- <query> <file_path>

Or build a release binary:

cargo build --release
.
# Executable is in target\release\minigrep.exe

CLI usage

Basic usage:

minigrep <query> <file_path>
  • query: the substring to search for
  • file_path: path to the file to search

Exit codes:

  • 0 on success
  • non-zero if there was an error reading the arguments or file

Environment variables

The CLI supports one environment variable to control case sensitivity:

  • IGNORE_CASE — if this environment variable is set (to any value), the search is performed case-insensitively. By default, the search is case-sensitive.

PowerShell example (enable case-insensitive search in the current session):

$env:IGNORE_CASE = "1"
cargo run -- rUsT poem.txt
# Remove variable from session if desired
Remove-Item Env:\IGNORE_CASE

Library usage

The crate exposes two functions in src/lib.rs that are useful when embedding the search logic into other Rust programs:

  • minigrep::search(query: &str, contents: &str) -> Vec<&str> — case-sensitive search
  • minigrep::search_case_insensitive(query: &str, contents: &str) -> Vec<&str> — case-insensitive search

Example (using the crate as a local dependency or from path):

use minigrep::search;

fn main() {
	let text = "Rust:\nsafe, fast, productive.";
	let matches = search("duct", text);
	assert_eq!(matches, vec!["safe, fast, productive."]);
}

Examples

Assuming the included poem.txt file is in the repository root:

PowerShell (case-sensitive):

cargo run -- frog poem.txt

PowerShell (case-insensitive):

$env:IGNORE_CASE = "1"
cargo run -- rUsT poem.txt

Sample output (case-insensitive search for rUsT):

Rust:
Trust me.

Testing

Run unit tests with:

cargo test

The repository contains unit tests for both case-sensitive and case-insensitive search functions in src/lib.rs.

Development notes

  • CLI argument parsing and config are implemented in src/main.rs (the Config struct and Config::build).
  • The run function reads the file and delegates to the appropriate search function based on the IGNORE_CASE environment variable.

Contributing

Contributions are welcome. Please open an issue or submit a pull request with a clear description of the change.

Ideas for follow-ups:

  • Add regex support
  • Add recursive directory search
  • Add colored output and line numbers

License

No license is specified in Cargo.toml. If you plan to reuse or publish this code, consider adding a license field to Cargo.toml (for example, MIT or Apache-2.0) and including a corresponding LICENSE file.


If anything in this README should be expanded (examples, tests, formatting), tell me what you'd like and I can add it.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages