I'm having difficulty with Iterator
's flat_map
function and am not quite sure how to understand and tackle this compiler error.
I'm flat_mapping a list of file paths into two strings by serializing two structs:
let body: Vec<String> = read_dir(query.to_string())
.iter()
.enumerate()
.flat_map(|(i, path)| {
let mut body: Vec<String> = Vec::with_capacity(2);
let entry = Entry { i };
body.push(serde_json::to_string(&entry).unwrap());
let record = parse_into_record(path.to_string()).unwrap();
body.push(serde_json::to_string(&record).unwrap());
body.iter()
})
.collect();
error[E0277]: a value of type `std::vec::Vec<std::string::String>` cannot be built from an iterator over elements of type `&std::string::String`
--> src/main.rs:275:10
|
275 | .collect();
| ^^^^^^^ value of type `std::vec::Vec<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::vec::Vec<std::string::String>`