У меня есть два HashSet<u16>
с, и я хотел бы реализовать a = a U b
.Если возможно, я бы хотел использовать HashSet::union
вместо циклов или других настроек.
Я попробовал следующее:
use std::collections::HashSet;
let mut a: HashSet<u16> = [1, 2, 3].iter().cloned().collect();
let b: HashSet<u16> = [7, 8, 9].iter().cloned().collect();
// I can build a union object that contains &u16
let union: HashSet<&u16> = a.union(&b).collect();
// But I can't store the union into a
a = a.union(&b).collect(); // -> compile error
// of course I can do
for x in &b {
a.insert(*x);
}
// but I wonder if union could not be used to simply build a union
Сообщение об ошибке следующее:
the trait bound
`std::collections::HashSet<u16>: std::iter::FromIterator<&u16>`
is not satisfied
Как мне выполнить a = a U a
?