Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anacrolix/902760b8509f43f35d027b4b771ce72b to your computer and use it in GitHub Desktop.
Save anacrolix/902760b8509f43f35d027b4b771ce72b to your computer and use it in GitHub Desktop.
trait ExtendValues<T> {
fn extend_values<I>(&mut self, iter: I)
where
I: Iterator<Item = T>;
}
impl<K, V, E, T> ExtendValues<(K, E)> for HashMap<K, V>
where
K: Eq + std::hash::Hash,
V: Default + Extend<T>,
E: IntoIterator<Item = T>,
{
fn extend_values<I>(&mut self, iter: I)
where
I: IntoIterator<Item = (K, E)>,
{
for (k, e) in iter {
self.entry(k).or_default().extend(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment