From ee9858663c5527065a219d2ac9abfb6c478bb036 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Sat, 11 Mar 2023 23:25:49 +0000 Subject: [PATCH] Up to 12.5 --- src/lib.rs | 32 +++++++++++++++++++++++++++++++- src/main.rs | 4 ++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84dd812..5e503e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,37 @@ impl Config { pub fn run(config: Config) -> Result<(), Box> { let contents = fs::read_to_string(config.file_path)?; - println!("With text:\n{}", contents); + for line in search(&config.query, &contents) { + println!("{line}"); + } Ok(()) } + +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 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn one_result() { + let query = "duct"; + let contents = "\ +Rust: +safe, fast, productive. +Pick tthree."; + + assert_eq!(vec!["safe, fast, productive."], search(query, contents)) + } +} diff --git a/src/main.rs b/src/main.rs index bb73f0d..aef3e0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,8 @@ fn main() { process::exit(1); }); - println!("Searching for {}", config.query); - println!("In file {}", config.file_path); + // println!("Searching for {}", config.query); + // println!("In file {}", config.file_path); if let Err(e) = minigrep::run(config) { println!("Application error: {e}");