// compile with: rustc -O wpb.rs // splits input into tokens separated by ASCII whitespace, counts // them and outputs them with counts in ascending order use std::collections::HashMap; use std::io::{self, BufRead, Write}; fn main() -> io::Result<()> { let mut words = HashMap::, u64>::new(); { let stdin = io::stdin(); for line in io::BufReader::new(stdin.lock()).split(b'\n') { let line = line?; for word in line.split(u8::is_ascii_whitespace) { if !word.is_empty() { *words.entry(Vec::from(word)).or_default() += 1; } } } } let mut list = words .into_iter() .map(|(token, amount)| (amount, token)) .collect::>(); // this also sorts by token (if amount is equal) list.sort(); // rust stdio is always line buffered (even non-tty output); use buffer on top. { let stdout = io::stdout(); let mut stdout = io::BufWriter::new(stdout.lock()); for (amount, token) in list { write!(stdout, "{}\t", amount)?; stdout.write_all(&token)?; stdout.write_all(b"\n")?; } } Ok(()) }