Converter
Harsh to Rust, or Rust to Harsh -- the transpiler and the converter themselves, running in your browser; the Harsh a Rust file becomes here is what hrs-from writes. Run opens the Rust side on the Rust Playground. For the matrix sample, use the Playground, which sends the part of hrs_std a program uses along with it.
Harsh
RustRun ↗
use std::collections::HashMap;
/// Count how often each word appears.
fn count(words: &[&str]) -> HashMap<String, usize> {
let mut counts = HashMap::new();
for w in words {
*counts.entry(w.to_lowercase())
.or_insert(0) += 1
}
counts
}
fn main() {
let text = "the fox and the dog and the cat";
let words: Vec<&str> = text.split_whitespace()
.collect();
let counts = count(&words);
let mut top: Vec<_> = counts.iter()
.collect();
top.sort_by(|a, b| {
b.1
.cmp(a.1)
.then(a.0.cmp(b.0))
});
for (word, n) in top.iter().take(3) {
println!("{word}: {n}")
}
}