Search contents using iterators
This commit is contained in:
parent
cbca3bd0d8
commit
73426dd5e0
1 changed files with 8 additions and 17 deletions
|
@ -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> {
|
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
let mut results = Vec::new();
|
contents
|
||||||
|
.lines()
|
||||||
for line in contents.lines() {
|
.filter(|line| line.contains(query))
|
||||||
if line.contains(query) {
|
.collect()
|
||||||
results.push(line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
|
||||||
let query = query.to_lowercase();
|
let query = query.to_lowercase();
|
||||||
let mut results = Vec::new();
|
|
||||||
|
|
||||||
for line in contents.lines() {
|
contents
|
||||||
if line.to_lowercase().contains(&query) {
|
.lines()
|
||||||
results.push(line);
|
.filter(|line| line.to_lowercase().contains(&query))
|
||||||
}
|
.collect()
|
||||||
}
|
|
||||||
|
|
||||||
results
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue