jonhoo / hashbag

An unordered multiset/bag implementation backed by HashMap
Apache License 2.0
65 stars 8 forks source link

How to turn a (T, usize) collection into a HashBag? #14

Closed myarcana closed 1 year ago

myarcana commented 1 year ago
let mut bag = HashBag::new();
bag.insert("a");
bag.insert("b");
bag.insert("b");

let bag_as_vec = bag.set_iter().collect::<Vec<(&&str, usize)>>();

Is there a way to turn bag_as_vec back into a HashBag?

And how about for collections or iterators in general with types like Vec<T, usize> (that didn't come from HashBag::SetIter) ?

jonhoo commented 1 year ago

HashBag implements Extend<(T, usize)>, so you should be able to just:

let mut bag = HashBag::new();
bag.extend(bag_as_vec);