Up to 12.5

This commit is contained in:
Ada Werefox 2023-03-11 23:25:49 +00:00
parent c56cdc37fc
commit ee9858663c
2 changed files with 33 additions and 3 deletions

View File

@ -22,7 +22,37 @@ impl Config {
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
let contents = fs::read_to_string(config.file_path)?;
println!("With text:\n{}", contents);
for line in search(&config.query, &contents) {
println!("{line}");
}
Ok(())
}
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
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_result() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick tthree.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents))
}
}

View File

@ -11,8 +11,8 @@ fn main() {
process::exit(1);
});
println!("Searching for {}", config.query);
println!("In file {}", config.file_path);
// println!("Searching for {}", config.query);
// println!("In file {}", config.file_path);
if let Err(e) = minigrep::run(config) {
println!("Application error: {e}");