From 42f7dc3ea45a1bbe80bef9a7557523bb7b447f1d Mon Sep 17 00:00:00 2001 From: Wilk Maia Date: Mon, 12 Feb 2018 18:26:48 -0300 Subject: [PATCH] lib: use iterators methods on search functions --- src/lib.rs | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d6cd50..113bb4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,28 +58,15 @@ pub fn run(config: Config) -> Result<(), Box> { } pub fn search_case_sensitive<'a>(pattern: &str, text: &'a str) -> Vec<&'a str> { - let mut result = vec![]; - - for line in text.lines() { - if line.contains(pattern) { - result.push(line); - } - } - - result + text.lines() + .filter(|line| line.contains(pattern)) + .collect() } pub fn search_case_insensitive<'a>(pattern: &str, text: &'a str) -> Vec<&'a str> { - let pattern = pattern.to_lowercase(); - let mut result = vec![]; - - for line in text.lines() { - if line.to_lowercase().contains(&pattern) { - result.push(line); - } - } - - result + text.lines() + .filter(|line| line.to_lowercase().contains(&pattern.to_lowercase())) + .collect() } #[cfg(test)]