Skip to content

Commit 7956720

Browse files
feat: Improving minigrep using iterators
1 parent 86e85f1 commit 7956720

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

rustlang_book/minigrep/src/lib.rs

+25-35
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ pub struct Config {
99
}
1010

1111
impl Config {
12-
pub fn build(args: &[String]) -> Result<Config, &'static str> {
13-
if args.len() < 3 {
14-
return Err("Not enough arguments. ");
15-
}
12+
pub fn build(mut args: impl Iterator<Item = String>) -> Result<Config, &'static str> {
13+
// The first value in the arguments is the name of the program
14+
// We dont use this so we get rid of it
15+
args.next();
1616

17-
let query = args[1].clone();
18-
let file_path = args[2].clone();
17+
let query = match args.next() {
18+
Some(arg) => arg,
19+
None => return Err("Didn't get a query string"),
20+
};
21+
22+
let file_path = match args.next() {
23+
Some(file_path) => file_path,
24+
None => return Err("Didn't get a file path"),
25+
};
1926

2027
let ignore_case = env::var("IGNORE_CASE").is_ok();
2128

@@ -44,28 +51,17 @@ pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
4451
}
4552

4653
pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
47-
let mut results = Vec::new();
48-
49-
for line in contents.lines() {
50-
if line.contains(query) {
51-
results.push(line);
52-
}
53-
}
54-
55-
return results;
54+
contents
55+
.lines()
56+
.filter(|line| line.contains(query))
57+
.collect()
5658
}
5759

5860
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
59-
let query = query.to_lowercase();
60-
let mut results = Vec::new();
61-
62-
for line in contents.lines() {
63-
if line.to_lowercase().contains(&query) {
64-
results.push(line);
65-
}
66-
}
67-
68-
return results;
61+
contents
62+
.lines()
63+
.filter(|line| line.to_lowercase().contains(query.to_lowercase().as_str()))
64+
.collect()
6965
}
7066

7167
#[cfg(test)]
@@ -75,23 +71,17 @@ mod tests {
7571
#[test]
7672
fn case_sensitive() {
7773
let query = "duct";
78-
let contents = "\
79-
Rust:
80-
safe, fast, productive.
81-
Pick three.
82-
Duct tape.";
74+
75+
let contents = "Rust:\nsafe, fast, productive.\nPick three.\nDuct tape.";
8376

8477
assert_eq!(vec!["safe, fast, productive."], search(query, contents));
8578
}
8679

8780
#[test]
8881
fn case_insensitive() {
8982
let query = "rUsT";
90-
let contents = "\
91-
Rust:
92-
safe, fast, productive.
93-
Pick three.
94-
Trust me.";
83+
84+
let contents = "Rust:\nsafe, fast, productive.\nPick three.\nTrust me.";
9585

9686
assert_eq!(
9787
vec!["Rust:", "Trust me."],

rustlang_book/minigrep/src/main.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use std::{env, process};
22

33
use minigrep::Config;
44
fn main() {
5-
let args: Vec<String> = env::args().collect();
6-
7-
let config = Config::build(&args).unwrap_or_else(|err| {
5+
let config = Config::build(env::args()).unwrap_or_else(|err| {
86
eprintln!("Problem parsing arguments: {}", err);
97
process::exit(1);
108
});

0 commit comments

Comments
 (0)