Avatar ·

How to correctly return an iterator (or any other trait) from a function in Rust?

Hi everyone! I started learning Rust and ran into a problem that seems to plague all beginners. I want to write a function that returns an iterator. For example, I want to create a function that filters numbers and returns only even ones: ```rust fn get_even(numbers: Vec) -> Iterator { numbers.into_iter().filter(|x| x % 2 == 0) } ``` But the compiler gives an error: "the trait `Iterator` cannot be made into an object". I've read about `dyn` and `impl`, but I don't fully understand which option is correct. I tried using `impl Iterator`: ```rust fn get_even(numbers: Vec) -> impl Iterator { numbers.into_iter().filter(|x| x % 2 == 0) } ``` This works, but I can't understand: when should I use `impl Trait` and when `Box`?