I was diving into Rust head-first again now, making a function to remove every other element:
fn remove_every_other(arr: &[u8]) -> Vec<u8> {
arr.iter().step_by(2).collect::<Vec<_>>()
}
But then I was hit with this error:
|
1 | fn remove_every_other(arr: &[u8]) -> Vec<u8> {
| ------- expected `Vec<u8>` because of return type
2 | arr.iter().step_by(2).collect::<Vec<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u8`, found `&u8`
|
= note: expected struct `Vec<u8>`
found struct `Vec<&u8>`
That's an easy fix for me, just map and dereference, right?
arr.iter().step_by(2).map(|x| *x).collect::<Vec<_>>()
However, I don't think this is the right way... it feels dirty.
I also tried using into_iter
because in the three forms of iteration, it was listed to return T
and not &T
, but using it gives me the same error(?).
Is there a "cleaner" or better way to do this? I was unable to find the right terms to search for this, so if you've got the question I need to see, I'll gladly close as a duplicate :)