# How to write trait which returns an iterator

**URL:** <https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828>\
**Category:** help\
**Created:** [June 24, 2020, 8:20am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828 "2020-06-24T08:20:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilLord](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@PhilLord](https://users.rust-lang.org/u/PhilLord)\
**Post date:** [June 24, 2020, 8:20am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/1 "2020-06-24T08:20:47Z")

</div>

I am struggling a bit with generics, traits and return types.

Imagine a simple function like this. I can use `impl` here to define  
the return type.

```rust
fn retn_vec_iter(v:&Vec<isize>) -> impl Iterator<Item=&isize>{
    v.iter()
}

```

Now imagine I want to define this as a trait.

```rust
trait Return {
    fn retn_vec_iter<'a, I>(v:&'a Vec<isize>) -> impl Iterator<Item=&'a isize>;
}

```

I am not allowed to do this unfortunately.

```rust
error[E0562]: `impl Trait` not allowed outside of function and inherent method return types
 --> src/main.rs:6:57
  |
6 | fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> impl Iterator<Item=&'a isize>;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

```

So, I have tried this instead.

```rust
trait Return {
    fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> I
        where I: Iterator<Item=&'a isize>;
}

```

This compiles but if I try to implement it, it fails:

```rust
fn retn_vec_iter(v:&Vec<isize>) -> impl Iterator<Item=&isize>{
    v.iter()
}

trait Return {
    fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> I
        where I: Iterator<Item=&'a isize>;
}

struct Empty;

impl Return for Empty {
    fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> I
        where I: Iterator<Item=&'a isize> {
        retn_vec_iter(v)
    }
}

```

as the generic version does not have the same return type as the bare  
function `retn_vec_iter`

```rust
   |
1 | fn retn_vec_iter(v:&Vec<isize>) -> impl Iterator<Item=&isize>{
   | -------------------------- the found opaque type
...
13 | fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> I
   | - this type parameter - expected `I` because of return type
14 | where I: Iterator<Item=&'a isize> {
15 | retn_vec_iter(v)
   | ^^^^^^^^^^^^^^^^ expected type parameter `I`, found opaque type
   |
   = note: expected type parameter `I`
                 found opaque type `impl std::iter::Iterator`

```

I think I am doing something wrong here. How do I add a method to  
trait that returns an iterator of some type?

---

<div class="post-metadata">

**Author:** ![RAOF](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/raof/32/14459_2.png) [@RAOF](https://users.rust-lang.org/u/RAOF)\
**Post date:** [June 24, 2020, 9:12am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/2 "2020-06-24T09:12:36Z")

</div>

When you write

```rust
trait Return {
    fn retn_vec_iter<'a, I>(&self, v:&'a Vec<isize>) -> I
        where I: Iterator<Item=&'a isize>;
}

```

You're saying “For whatever type `I` (that implements `Iterator`) the calling code chooses, `retn_vec_iter` will return a value of type `I`”

You probably can't write that function. The error message you're getting says “you promised to return a value of type `I`, chosen by the caller, but you're returning this fixed type instead”.

What you _want_ to say (and what `-> impl Iterator` means) is “`return_vec_iter` returns _some_ fixed type. I'm not telling you exactly what it is, but that type implements `Iterator`”.

You can't do _exactly_ that in a trait, but you can do the next best thing, which is to have an associated type on the trait. That lets each implementation specify the particular concrete type that its implementation of `retn_vec_iter` returns.

[The declaration of `IntoIterator`](https://doc.rust-lang.org/std/iter/trait.IntoIterator.html), which is similar to what you want to do, might help.

---

<div class="post-metadata">

**Author:** ![droundy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/droundy/32/1129_2.png) [@droundy](https://users.rust-lang.org/u/droundy)\
**Post date:** [June 24, 2020, 2:04pm UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/3 "2020-06-24T14:04:38Z")

</div>

The other option would be to return a `&dyn Iterator`. This would be appropriate if there allowed of this iterator is not critical, and simplicity of coding is more important.

---

<div class="post-metadata">

**Author:** ![PhilLord](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@PhilLord](https://users.rust-lang.org/u/PhilLord)\
**Post date:** [June 24, 2020, 6:38pm UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/4 "2020-06-24T18:38:32Z")

</div>

> [@RAOF](#):
>
> ait, but you can do the next best thing, which is to have an associated type on the trait. That lets each implementation specify the particular concrete type that its implementation of `retn_vec_iter` returns.

Okay, yes, that is good! So I can do this.

```rust
fn retn_vec_iter<'a>(v:&'a Vec<isize>) -> std::slice::Iter<'a, isize>{
    v.iter()
}

trait Return<'a> {
    type Iter: Iterator<Item=&'a isize>;
    fn retn_vec_iter(&self, v:&'a Vec<isize>) ->
        Self::Iter;
}

struct Empty;

impl<'a> Return<'a> for Empty {
    type Iter = std::slice::Iter<'a,isize>;
    fn retn_vec_iter(&self, v:&'a Vec<isize>) -> Self::Iter {
        retn_vec_iter(v)
    }
}

pub fn main() {

}

```

What if I want to add some implemented methods to the trait. Say I  
extend it to:

```rust
fn retn_vec_even_iter(&self, v:&'a Vec<isize>) -> Self::Iter {
        self.retn_vec_iter(v).filter(|n| *n % 2 == 0)
}

```

```rust
trait Return<'a> {
    type Iter: Iterator<Item=&'a isize>;
    type EvenIter: Iterator<Item=&'a isize>;
    fn retn_vec_iter(&self, v:&'a Vec<isize>) ->
        Self::Iter;

    fn retn_vec_even_iter(&self, v:&'a Vec<isize>) -> Self::EvenIter {
        self.retn_vec_iter(v).filter(|n| *n % 2 == 0)
    }
}

```

which gives me:

```rust
  = note: consider constraining the associated type `<Self as Return<'a>>::EvenIter` to `std::iter::Filter<<Self as Return<'a>>::Iter, [closure@src/main.rs:13:38: 13:53]>` or calling a method that returns `<Self as Return<'a>>::EvenIter`

```

I cannot figure out the syntax this is suggesting? And do I need another associated type or can I reuse them (i.e. do I need `EvenIter` here)?

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/bjorn3/32/5255_2.png) [@bjorn3](https://users.rust-lang.org/u/bjorn3)\
**Post date:** [June 24, 2020, 7:32pm UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/5 "2020-06-24T19:32:19Z")

</div>

Here you are saying that the implementor of `Return` is allowed to pick any `EvenIter` that is an iterator and `retn_vec_even_iter` will return that specific type. Something like the following should work:

```rust
trait Return<'a> {
    type Iter: Iterator<Item=&'a isize>;
    type EvenIter: Iterator<Item=&'a isize>;
    fn retn_vec_iter(&self, v:&'a Vec<isize>) -> Self::Iter;

    fn retn_vec_even_iter(&self, v:&'a Vec<isize>) -> std::iter::Filter<Self::Iter, fn(&&isize) -> bool> {
        self.retn_vec_iter(v).filter(is_even)
    }
}

fn is_even(i: &&isize) -> bool {
    *i % 2 == 0
}

```

---

<div class="post-metadata">

**Author:** ![PhilLord](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@PhilLord](https://users.rust-lang.org/u/PhilLord)\
**Post date:** [June 24, 2020, 9:08pm UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/6 "2020-06-24T21:08:37Z")

</div>

> [@bjorn3](#):
>
> ```rust
> fn is_even(i: &&isize) -> bool {
> *i % 2 == 0
> }
> 
> ```

Don't understand that. `EvenIter` doesn't appear anywhere except in the type declaration.

---

<div class="post-metadata">

**Author:** ![2e71828](https://avatars.discourse-cdn.com/v4/letter/2/3e96dc/32.png) [@2e71828](https://users.rust-lang.org/u/2e71828)\
**Post date:** [June 24, 2020, 10:27pm UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/7 "2020-06-24T22:27:49Z")

</div>

> [@PhilLord](#):
>
> Don't understand that. `EvenIter` doesn't appear anywhere except in the type declaration.

`EvenIter` isn’t being used at all anymore and that line can be safely removed. The return type `std::iter::Filter<Self::Iter, fn(&&isize) -> bool>` is the full type returned by [`Iterator::filter()`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.filter)

---

<div class="post-metadata">

**Author:** ![daboross](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/daboross/32/43777_2.png) [@daboross](https://users.rust-lang.org/u/daboross)\
**Post date:** [June 25, 2020, 2:47am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/8 "2020-06-25T02:47:06Z")

</div>

Writing a definition which uses one associated type per function and has default function definitions would require default associated types to work well. Unfortunately, it's still unstable (and I don't think even implemented unstably?).

There's more details on that [here](https://github.com/rust-lang/rust/issues/29661), but the gist is that what you'd really want is a way to say "the default implementation of `retn_vec_even_iter` depends on a default type for `type EvenIter`. If you override one, you must override the other". The above feature would introduce a good way to do this, but right now there isn't any.

There are a few ways to work around this, but none are ideal.

@bjorn3's solution is to disregard trait implementors' ability to write better implementations. This is a reasonable tradeoff - it's what `Iterator` itself does, for instance. You can't implement `Iterator` and change `Iterator::filter` to return your own special better type - it always returns an `std::iter::Filter`.

Another option would be to remove the default implementation. You'd add more boilerplate, but implementors would get to implement it however they way.

The third would be to just return `Box<dyn Iterator<Item=&'a isize>>` from `retn_vec_even_iter`. It'd be replacing static dispatch with dynamic dispatch, so you get a slight performance hit, but you can make a default impl and implementors can also override it and supply their own type. If this code isn't a hot path, this is probably what I'd recommend going with.

---

<div class="post-metadata">

**Author:** ![PhilLord](https://avatars.discourse-cdn.com/v4/letter/p/258eb7/32.png) [@PhilLord](https://users.rust-lang.org/u/PhilLord)\
**Post date:** [June 25, 2020, 8:20am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/9 "2020-06-25T08:20:42Z")

</div>

Indeed, the compromise of @bjorns3 solution is not good for me. In my real code (I have shown smaller exemplars because my own is too complex), I am hitting these problems because I am replacing what was a concretely implemented struct with a trait because the performance of the default implementation is catastrophically too slow. I would remove the boilerplate indeed, but my current struct has around 30 methods. So if I accept the limitation of a concrete type, there is not point to writing the trait in the first place.

I could remove the default, but I have about thirty of them, so it would be quite a bit hassle to re-implement them all. And, unfortunately, the code is hot path -- that's why I am rewriting it. I guess I could macro this out -- the default implementation is already macrod so that would not be a disaster.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 23, 2020, 8:20am UTC](https://users.rust-lang.org/t/how-to-write-trait-which-returns-an-iterator/44828/10 "2020-09-23T08:20:48Z")

</div>

Closed on September 23, 2020, 8:20am UTC

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.
