How to correctly return an iterator (or any other trait) from a function in Rust?
Hello, colleagues! I started learning Rust and encountered a problem that seems very strange to me. I'm trying to write a function that returns an iterator, but the compiler keeps complaining.
Here's what I want to do: I have a vector of numbers, and I want to filter even numbers and return an iterator over them. I wrote this code:
```rust
fn get_even_numbers(numbers: Vec) -> Iterator- {
numbers.into_iter().filter(|&x| x % 2 == 0)
}
```
But the compiler gives an error:
```
error[E0277]: the size for values of type `(dyn Iterator
- + 'static)` cannot be known at compilation time
```
I understand that traits are not fixed-size types, but how do I correctly return an iterator? I've seen that in some places `Box
>` is used,
No answers yet.
Log in to leave an answer