diff options
author | 53hornet <53hornet@gmail.com> | 2019-02-25 21:57:25 -0500 |
---|---|---|
committer | 53hornet <53hornet@gmail.com> | 2019-02-25 21:57:25 -0500 |
commit | 3402ea2a8c91e187afdb5e8ca085d779f5877674 (patch) | |
tree | 33fe01879c3575dd8747d81becb02dfee713fdd2 /minigrep/src | |
parent | b7dd584bca69dc729d77b11b6a8378b8be0af647 (diff) | |
download | learning-rust-3402ea2a8c91e187afdb5e8ca085d779f5877674.tar.xz learning-rust-3402ea2a8c91e187afdb5e8ca085d779f5877674.zip |
Added first test.
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); |