//! Counting, kept apart from reading so it can be tested. /// What a text holds. #[derive(Debug, Default, PartialEq)] pub struct Counts { pub lines: usize, pub words: usize, pub bytes: usize, } /// Counts `text` in one pass. pub fn count(text: &str) -> Counts { let mut counts = Counts { bytes: text.len(), ..Counts::default() }; for line in text.lines() { counts.lines += 1; counts.words += line.split_whitespace().count(); } counts } #[cfg(test)] mod tests { use super::*; #[test] fn counts() { let c = count("one two\nthree\n"); assert_eq!(c, Counts { lines: 2, words: 3, bytes: 14 }); } }