This commit is contained in:
2026-01-23 08:53:52 +00:00
parent c258b53e52
commit fd51666ea8
4 changed files with 152 additions and 0 deletions

6
minigrep/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "minigrep"
version = "0.1.0"
edition = "2024"
[dependencies]

10
minigrep/poems.txt Normal file
View File

@@ -0,0 +1,10 @@
I'm nobody! Who are you?
Are you nobody, too?
Then there's a pair of us - don't tell!
They'd banish us, you know.
How dreary to be somebody!
How public, like a frog
To tell your name the livelong day
To an admiring bog!

102
minigrep/src/lib.rs Normal file
View File

@@ -0,0 +1,102 @@
use std::env;
use std::error::Error;
use std::fs;
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
let mut results;
let casing = (config.ignore_case_arg, config.ignore_case_env);
// only perform case sensitive search if both
// ignore_case_env and ignore_case_arg are false
match casing {
(false, false) => results = search(&config.query, &contents),
(_, _) => results = search_case_insensitive(&config.query, &contents),
}
for line in results {
println!("{line}");
}
Ok(())
}
pub struct Config {
pub query: String,
pub file_path: String,
pub ignore_case_arg: bool,
pub ignore_case_env: bool,
}
impl Config {
pub fn build(args: &[String]) -> Result<Config, &'static str> {
if args.len() < 4 {
return Err("not enough arguments");
}
let query = args[1].clone();
let file_path = args[2].clone();
let ignore_case_arg = !args[3].clone().is_empty();
let ignore_case_env = env::var("IGNORE_CASE").is_ok();
Ok(Config {
query,
file_path,
ignore_case_arg,
ignore_case_env,
})
}
}
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}
results
}
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();
let query = query.to_lowercase();
for line in contents.lines() {
if line.to_lowercase().contains(&query) {
results.push(line);
}
}
results
}
#[cfg(test)]
mod tests {
use super::*;
#[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)
);
}
}

34
minigrep/src/main.rs Normal file
View File

@@ -0,0 +1,34 @@
use minigrep::Config;
use std::env;
use std::process;
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|error| {
println!("Problem parsing arguments: {error}");
process::exit(1);
});
if let Err(e) = minigrep::run(config) {
println!("Application error: {e}");
process::exit(1);
}
}
// end of main
// another way to handle the Config building
// let config = Config::build(&args);
// match config {
// Err(error) => {
// println!("error: {error}");
// }
// Ok(config) => {
// println!(
// "searching for \"{}\" in file \"{}\"",
// config.query, config.file_path,
// );
//
// println!("with text:\n{contents}");
// }
// }