Up to 12.5
This commit is contained in:
parent
c56cdc37fc
commit
ee9858663c
32
src/lib.rs
32
src/lib.rs
@ -22,7 +22,37 @@ impl Config {
|
|||||||
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
|
||||||
let contents = fs::read_to_string(config.file_path)?;
|
let contents = fs::read_to_string(config.file_path)?;
|
||||||
|
|
||||||
println!("With text:\n{}", contents);
|
for line in search(&config.query, &contents) {
|
||||||
|
println!("{line}");
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,8 +11,8 @@ fn main() {
|
|||||||
process::exit(1);
|
process::exit(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
println!("Searching for {}", config.query);
|
// println!("Searching for {}", config.query);
|
||||||
println!("In file {}", config.file_path);
|
// println!("In file {}", config.file_path);
|
||||||
|
|
||||||
if let Err(e) = minigrep::run(config) {
|
if let Err(e) = minigrep::run(config) {
|
||||||
println!("Application error: {e}");
|
println!("Application error: {e}");
|
||||||
|
Reference in New Issue
Block a user