diff options
Diffstat (limited to 'minigrep/src')
-rw-r--r-- | minigrep/src/lib.rs | 31 | ||||
-rw-r--r-- | minigrep/src/main.rs | 2 |
2 files changed, 30 insertions, 3 deletions
diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs index 78610ba..b55e924 100644 --- a/minigrep/src/lib.rs +++ b/minigrep/src/lib.rs @@ -23,7 +23,36 @@ impl Config { pub fn run(config: Config) -> Result<(), Box<dyn Error>> { let contents = fs::read_to_string(config.filename)?; - dbg!(contents); Ok(()) } +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 three."; + + assert_eq!( + vec!["safe, fast, productive."], + search(query, contents) + ); + } +} diff --git a/minigrep/src/main.rs b/minigrep/src/main.rs index fc5ae9a..b1581c9 100644 --- a/minigrep/src/main.rs +++ b/minigrep/src/main.rs @@ -10,8 +10,6 @@ fn main() { println!("Problem parsing arguments: {}", err); process::exit(1); }); - println!("\nSearching for {}", config.query); - println!(" in file {}", config.filename); if let Err(e) = minigrep::run(config) { println!("Application error: {}", e); process::exit(1); |