I'm trying to impl an iterator over a Grid structure. The iterator must outlive the grid, and I've implemented almost everything - the problem I've got is that the compiler is complaining about the lifetime in the line:
the lifetime parameter 'a is not constrained by the impl trait, self type, or predicates
unconstrained lifetime parameter
I don't understand a.) why it's showing up (I've included constraints in the methods within the impl block) or b.) how to fix. I thought I needed to include lifetimes because my iterator is non-owning - it's referencing back to the owned Vec - using its .iter() method. Is the fix to add a lifetime parameter to my Grid and then everything else that that references... (real pain)?
Then the iterator must return owned values, T not &'a T. An Iterator cannot return borrows of its own data. The “unconstrained lifetime” is a symptom of this problem: the struct and impl Iterator are written so that 'awould be the lifetime of what it's borrowing, but the impl IntoIterator hasn't got anything specific to borrow to create the iterator.
This code will not compile regardless of lifetime annotations, because self.values.iter() borrows self, but self is being dropped at the end of the function. You have to work with self.values.into_iter() instead, as long as you want the iterator to outlive the grid.
Here's a complete fixed version of your code. Note that I also replaced dyn Iterator — being a dynamically-sized type, it would have prevented actually constructing the struct unless you changed it to have a Box wrapper, and you don't really need dyn here.