How to collect iterator to slice of string slices to vec of string slices


I’ve just started learning rust and have simple problem.

I have a simple slice of string slices like:
let a : &[&str] = &[“hello”, “world”];

I want to use iterator with it (filter it using some condition) and then collect results into Vec of string slices.

Basically help me to compile code in play rust link. I understand the error and I can fix it with longer code like creating and populating Vec by myself, but I want to understand iterators API so I hope there should be simple solution


Try let v : Vec<&str> = a.into_iter().cloned().collect();. An iterator over a slice iterates over references to each of the elements, i.e. &&str, so you need to map from that to &str.

Edit: Err, actually, in this particular case, it’s probably simpler to just write a.to_vec().


Thank you! It works and now I even understand why