rustgrep/
lib.rs

1pub fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
2    for line in content.lines() {
3        if line.contains(&pattern.to_lowercase()) {
4            let result = writeln!(writer, "{}", line);
5            match result {
6                Ok(_) => (),
7                Err(e) => println!("Error in writing: {:?}", e)
8            }
9        }
10    }
11}
12
13
14#[test]
15fn should_find_matches() {
16    let mut result = Vec::new();
17    find_matches("lorem ipsum\ndolor sit amet", "lorem", &mut result);
18    assert_eq!(result, b"lorem ipsum\n");
19}
20
21#[test]
22fn should_find_zero_matches() {
23    let mut result = Vec::new();
24    find_matches("lorem ipsum\ndolor sit amet", "loram", &mut result);
25    assert_eq!(result, b"");
26}
27
28#[test]
29fn should_find_case_insensitive_matches() {
30    let mut result = Vec::new();
31    find_matches("lorem ipsum\ndolor sit amet", "LOREM", &mut result);
32    assert_eq!(result, b"lorem ipsum\n");
33}