let lines: Vec<&str> = contents.split_whitespace().collect();
let mut ans = HashSet::<String>::new();
for i in lines.iter() {
let f: String = read_all(*i);
let t: HashSet<String> = f.split_whitespace().map(|s| s.to_string()).collect::<HashSet<String>>();
ans = ans.union(&t).collect();
}
"contents" is a string of filenames, separated by \n.
For each file, we want to read in the entire file, split by whitespace, and put all the words into a hashset.
Then we want to union all the hashsets into one hashset.
In short: We have a collection of files. We want a HashSet of all words.
The problem I am running into is the union line. I am getting error:
|
66 | ans = ans.union(&t).collect();
| ^^^^^^^ a collection of type `std::collections::HashSet<std::string::String>` cannot be built from `std::iter::Iterator<Item=&std::string::String>`
|
= help: the trait `std::iter::FromIterator<&std::string::String>` is not implemented for `std::collections::HashSet<std::string::String>`
How about ans.extend(t) -- turns t into an iterator by value, then inserts all items into ans.
If your items have some significant property beyond equality, you might want to take more care whether the old or new value is preserved, but for String the only possible difference is capacity.
edit: in this case, you don't really even need the interim t at all. Just: