[Solved] Help me make my code fail to compile (unbounded lifetimes)

I'm trying to implement my own Iter struct with a Vec backing, and I'm having a terrible time trying to get lifetimes to behave properly. I've been studying the Iterator implementations in std, but I just can't seem to figure out where I'm going wrong. I feel like I've tried everything under the sun.

Here's a reduced case that should obviously fail to compile:

Any help is greatly appreciated.

To avoid the XY problem, my ultimate goal is to have the struct implement Iterator as well as a trait like Iterator, except that it steps backwards instead of forwards.

This trait is called DoubleEndedIterator in std an Vec's various iterators already implements it. You can turn a double-ended iterator into an ordinary iterator that steps backwards by using the .rev() method.

But to address the original question, which is also quite interesting: You can't both

  • Return references from Iterator::next and
  • Have the iterator own the collection it's iterating over.

If you do what slice iterators do (borrow the collection instead of owning it), you have something you can tie the Item's lifetime to. Instead of this, where the lifetime can be chosen arbitrarily by the caller:

impl <'a, T> GregIter<'a, T> where T: 'a {
    pub fn new(v: Vec<T>) -> Self {

the signature is the moral equivalent of

impl <'a, T> SliceIter<'a, T> where T: 'a {
    pub fn new(v: &'a [T]) -> Self {

Note that 'a occurs in the input so it can't be set to (say) 'static unless the backing collection is actually valid for 'static.

The alternative is to do what std::vec::IntoIter does, i.e. set type Item = T; instead of handing out references, then all lifetime concerns go out of the window.

Wow, so it's genuinely impossible to do the way I wanted! Thank you for the answer. Rev() doesn't actually work for my purposes for reasons that aren't worth getting into here, but I did discover the idea of using T as the Item. I'll have to live with some cloning, but oh well.

Thanks again.