Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterator collect issue with value of type `Vec<String>` cannot be built from `Iterator<Item=&String>` [duplicate]

Tags:

iterator

rust

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>`
like image 410
Miranda Abbasi Avatar asked Jan 15 '20 06:01

Miranda Abbasi


1 Answers

iter gives you an iterator over references. You need a consuming iterator which owns its values. For this, use into_iter instead. Here's a simple example:

fn main() {
    let result = (0..10).flat_map(|_| {
       let vec: Vec<String> = vec!["a".into(), "b".into(), "c".into()];
       vec.into_iter()
    }).collect::<Vec<_>>();
}

For a detailed explanation of the differences between iter and into_iter, refer to the following answer What is the difference between iter and into_iter?

like image 124
sshashank124 Avatar answered Nov 03 '22 00:11

sshashank124