Closures and trait bounds

fn double_positives<'a>(numbers: &'a Vec<i32>) -> impl Iterator<Item = i32> + 'a {
    numbers
        .iter()
        .filter(|x| x > &&0)
        .map(|x| x * 2)
}
fn main() {}

compile through

fn double_positives<'a,T>(numbers: &'a Vec<i32>) -> T
where T:Iterator<Item = i32> + 'a
{
    numbers
        .iter()
        .filter(|x| x > &&0)
        .map(|x| x * 2)
}
fn main() {}

Compilation failed
I probably know that it is because of the closure type, but is there a way to change it to where?

No.

You probably don't get what Alice means in another post. Return the closures - #2 by alice

The second function returns a type chosen by the caller, but actually chosen by the implementation inside the function.

So read Impl trait type - The Rust Reference especially Abstract return types and Differences between generics and impl Trait in return position parts.

4 Likes

No, that has a different meaning.

Also, don't post duplicates. This exact question of yours has been answered recently.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.