mirror of
https://github.com/darkicewolf50/RustBrock.git
synced 2025-08-02 08:10:53 -06:00
This commit is contained in:
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user