grrs_masterbongo/
lib.rs

1/// Instead of printing, write to Writer so its now testable
2#[allow(unused_must_use)]
3pub fn find_matches(content: &str, pattern: &str, mut writer: impl std::io::Write) {
4    // wrtier is whatever that implements std::io::Write
5    for line in content.lines() {
6        if line.contains(pattern) {
7            writeln!(writer, "{}", line);
8        }
9    }
10}
11
12#[cfg(test)]
13mod tests {
14    use crate::find_matches;
15
16    #[test]
17    fn find_a_match() {
18        // Vec<u8> can grow dynamically and implements std::io::Write
19        let mut result = Vec::new();
20        find_matches("test\nnot", "test", &mut result);
21        assert_eq!(result, b"test\n");
22    }
23}