I am working on my first Rust project, learning as I go. I am trying to filter a vector of &str, however when I attempt to collect the iterator I get this error:
a value of type `std::vec::Vec<&str>` cannot be built from an iterator over elements of type `&&str`
The documentation of filter does mention the situation where you get a double reference, following the advice in the documentation I have tried destructuring the references in the argument to the closure, I also have tried adding single or double dereference on the body of the closure.
let test = vec!("a", "b", "", "c");
let filt_a: Vec<&str> = tst.iter().filter(|&&x| x.is_empty()).collect();
let filt_b: Vec<&str> = tst.iter().filter(|&x| *x.is_empty()).collect();
let filt_c: Vec<&str> = tst.iter().filter(|x| **x.is_empty()).collect();
Any fewer dereferences (or destructured references) also result in exactly the same error.
Adding any additional dereferences just results in the error when you try and dereference a plain &str
.
Any advice to get me on the right path would be wonderful.