std::vec::Vec<std::string::String>> and collect


#1

I have a bit of code:

fn create_remove_list(data: HashMap<String, Vec<String>>) -> Vec<String> {
    data.iter().map(|it| {let x = it.1.clone(); x.sort_by(debian_version_number_comparator); x}).collect()
}

but I am getting the error:

  |
28 |     data.iter().map(|it| {let x = it.1.clone(); x.sort_by(debian_version_number_comparator); x}).collect()
   |                                                                                                  ^^^^^^^ the trait `std::iter::FromIterator<std::vec::Vec<std::string::String>>` is not implemented for `std::vec::Vec<std::string::String>`
   |
   = note: a collection of type `std::vec::Vec<std::string::String>` cannot be built from an iterator over elements of type `std::vec::Vec<std::string::String>`

Hopefully this is just me overlooking something really trivial.


#2

You can collect an iterator over Strings into a Vec<String>.
collecting an iterator over Vec<String> would give you an Vec<Vec<String>>.

EDIT:
You might want to have a look at flat_map.


#3

@troplin Aha, splendid, thanks. Of course I needed flat_map. Everything now up and running.