I was using Vec<usize>::for_each
to remove each matching element from a HashMap<usize, _>
. Since Hashmap::remove
returns Option<_>
for_each is unhappy with the return type of the closure, but I can't include a semi colon without a block so I either have to have a block or call drop. (In my use case I don't care whether the keys being removed exist)
Option 1:
vec.iter().for_each(|key| { hash_map.remove(key); });
Option 2:
vec.iter().for_each(|key| drop(hash_map.remove(key));
I know that both of these approaches work, but I'm curious about what people consider more idiomatic and whether there's any subtle differences I'm missing between the implementations, or whether there's another approach that I've missed. RustFMT will turn option 1 into three lines, which feels like unnecessary noise, but calling drop here feels like a strange distraction if future me needs to change this code.
X/Y Problem disclosure: I found HashMap::retain
which is probably a better fit for this problem (changing the Vec
to a HashSet
to remove the cost of multiple calls to Vec::contains
), but am still interested in the general question about closure return types.