I am trying to iterate over slices of a Vec so I can do a custom check to see if the element is a duplicate. To iterate over slices I am using the .window() method which returns an Iterator. However, Iterator.collect() fails in this case.
fn main() {
// Working
let v0: Vec<&str> = vec!["a", "b", "c"];
let v2: Vec<&str> = v0.clone().into_iter().collect();
assert_eq!(&v0, &v2);
// Not working, see error
let v3: Vec<&str> = v0.clone().windows(2).collect();
assert_eq!(&v0, &v3);
}
```rust
error[E0277]: a value of type `Vec<&str>` cannot be built from an iterator over elements of type `&[&str]`
--> src/main.rs:8:47
|
8 | let v3: Vec<&str> = v0.clone().windows(2).collect();
| ^^^^^^^ value of type `Vec<&str>` cannot be built from `std::iter::Iterator<Item=&[&str]>`
|
= help: the trait `FromIterator<&[&str]>` is not implemented for `Vec<&str>`
= help: the trait `FromIterator<&str>` is implemented for `Vec<&str>`
= help: for that trait implementation, expected `str`, found `[&str]`
note: required by a bound in `collect`
How do I get a Vec out of the Vec<&[T]> produced by .windows()?