finished ch12.4
All checks were successful
Test Gitea Actions / first (push) Successful in 22s

This commit is contained in:
2025-02-18 15:28:22 -07:00
parent e0bc88cf97
commit fae65591e6
5 changed files with 392 additions and 10 deletions

View File

@ -23,7 +23,39 @@ 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}")
// refactor 10
// 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> {
// original that only can fail
// vec![]
for line in contents.lines() {
if line.contains(query) {
// do something with line
}
}
}
#[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));
}
}

View File

@ -30,12 +30,13 @@ fn main() {
// process::exit(1);
// });
println!("Searching for {}", config.query);
println!("In the file {}", config.file_path);
// refactor 10
// println!("Searching for {}", config.query);
// println!("In the file {}", config.file_path);
// refactor 8
if let Err(e) = minigrep::run(config) {
// needed for helping the user
println!("Application error: {e}");
process::exit(1);
}