Rust Project: Minigrep

mini-grep

A simple, grep-like command-line tool written in Rust. It allows you to search for a specific string pattern within a text file.

Initial primary release! Will integrate more functions and capabilities in the future.

Features

  • Search for a text pattern in a specified file.
  • Case-sensitive search by default.
  • Case-insensitive search can be enabled via an environment variable.

Usage

To use mini-grep, you need to provide two arguments: a search pattern and the path to the file you want to search in.

Basic Search (Case-Sensitive)

To perform a case-sensitive search, run the program with the following command structure:

1
cargo run -- <pattern> <file_path>

Example:

1
cargo run -- "hello" poem.txt

This command will search for the exact word “hello” in the poem.txt file and print any lines that contain it.

You can perform a case-insensitive search by setting the IGNORE_CASE environment variable before running the command.

On Linux/macOS:

1
IGNORE_CASE=1 cargo run -- <pattern> <file_path>

On Windows (PowerShell):

1
$env:IGNORE_CASE=1; cargo run -- <pattern> <file_path>

Example:

1
IGNORE_CASE=1 cargo run -- "rUsT" poem.txt

This command will search for “rUsT” (and “rust”, “Rust”, “RUST”, etc.) in poem.txt and print all matching lines, regardless of their casing.

Todo List

  • feat: add iterator for function programming
  • Add recursive search for folders
  • Add Regex search
  • Add fuzzy search

Code

Just as a toy implementation… Still needs to be improved.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// main.rs
\use mini_grep::{Config, run};
use std::{env, process};

fn main() {
let config = Config::build(env::args()).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
println!("Searching for \"{}\" in file {}...", config.query, config.file_path);

if let Err(e) = run(config) {
eprintln!("Application error: {e}");
process::exit(1);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// lib.rs
use std::error::Error;
use std::{env, fs};
pub struct Config {
pub query: String,
pub file_path: String,
pub ignore_case: bool,
}
impl Config {
pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
args.next();
let query = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a query string"),
};
let file_path = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a file path"),
};
let ignore_case = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case,
})
}
}

pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents =
fs::read_to_string(config.file_path).expect("Should have been able to read the file");
let results = if config.ignore_case {
search_case_insensitive(&config.query, &contents)
} else {
search(&config.query, &contents)
};

for line in results {
println!("{line}");
}
Ok(())
}

// public core function: search for minigrep
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
contents
.lines()
.filter(|line| line.contains(&query))
.collect()
}

// public core function: search while case sensitive
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
contents
.lines()
.filter(|line| line.to_lowercase().contains(&query))
.collect()
}

// test module
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}

#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents)
);
}
}

Rust Project: Minigrep
https://xiyuanyang-code.github.io/posts/Rust-Project-Minigrep/
Author
Xiyuan Yang
Posted on
August 29, 2025
Updated on
September 2, 2025
Licensed under