gw_rust_programming_tutorial/chapter_12/
test_12_4.rs

1use std::{error::Error, fs};
2
3pub fn test_test_driver_dev()
4{
5    let r = run("c:\\2.txt");
6    if r.is_err() {
7        println!("error={:?}",r.err());
8    }
9}
10
11pub fn run(filename: &str) -> Result<(), Box<dyn Error>> {
12    let contents = fs::read_to_string(filename)?;
13
14    for line in search("sss", &contents) {
15        println!("{}", line);
16    }
17
18    Ok(())
19}
20
21pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
22    let mut results = Vec::new();
23
24    for line in contents.lines() {
25        if line.contains(query) {
26            results.push(line);
27        }
28    }
29
30    results
31}