I've been trying to get a handle on how the HashMap::entry() method works. The documentation is here.
I find the way the usage example, copied below, is written to be confusing (mainly because I'm still a novice at using Rust).
use std::collections::HashMap;
let mut letters = HashMap::new();
for ch in "a short treatise on fungi".chars() {
letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
}
assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
This line, in particular, is confusing to me:
for ch in "a short treatise on fungi".chars() {
letters.entry(ch).and_modify(|counter| *counter += 1).or_insert(1);
-
I'm not sure what function the pipe symbol
|
is serving. -
I could not find either the
and_modify()
method or theor_insert()
method in the documentation to help me follow how to applyentry()
.
If anyone could explain this to me I would appreciate it. Thanks.