diff --git a/minigrep/src/lib.rs b/minigrep/src/lib.rs index 1092248..1d93f05 100644 --- a/minigrep/src/lib.rs +++ b/minigrep/src/lib.rs @@ -50,28 +50,19 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> { } 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 + contents + .lines() + .filter(|line| line.contains(query)) + .collect() } pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> { let query = query.to_lowercase(); - let mut results = Vec::new(); - for line in contents.lines() { - if line.to_lowercase().contains(&query) { - results.push(line); - } - } - - results + contents + .lines() + .filter(|line| line.to_lowercase().contains(&query)) + .collect() }