The definition of a set union is that every element that occurs in either set is required to occur in the result. Therefore, the challenge is to insert elements from both sets into the resulting set, without creating duplicates.
Since this is handled by the insert process, a naive implementation could look like the following:
pub fn union(self, other: TrieSet<K>) -> TrieSet<K> {
let new = RefCell::new(TrieSet::new_empty());
self.walk(|k| new.borrow_mut().insert(k));
other.walk(|k| new.borrow_mut().insert(k));
new.into_inner()
}
This consumes both sets, returning only the result. The next operation, the intersection, looks very similar.