Thanks for building this great community and keeping it alive, and sorry if I’m duplicating an existing question!
So I’m trying to do some functional programming in Rust and I thought I’d use chained iterators instead of creating new vectors that are copies of the old vectors with just a single value added. I thought it’d be easy - pass an iterator as the “accumulator” parameter to a function, let the function create a iter::once() with the new value, chain the two and pass the result to the next iteration. Yeah, well, it turns out that I still don’t understand lifetimes in Rust very well (no big surprise there)
So for a simple demonstration of what I’m trying to do, here’s some working code without iterators:
And here’s what I’m actually trying to do::
The compiler complains that “digit” doesn’t live long enough, and I kind of understand its reasoning: obviously, “digit” is only valid until the function ends, but the 'a lifetime for the passed-in iterator is waaay longer. However, I do not want to use the 'a lifetime; how do I tell the compiler that the iter::once(&digit).chain(acc) iterator should be considered to have the same lifetime as digit, and not the same lifetime as the passed-in acc?
Or am I misunderstanding something, and is there a way that chaining two iterators could somehow leave a trace beyond the lifetime of the chain itself? I really don’t think it should matter - I’m only passing the buit-up chain to another instance of the convert() function, so I ought to be able to tell it to use the shorter lifetime of digit for the recursive invocation of convert()… shouldn’t I?
I tried playing around with HRTB a bit (both for and 'b: 'a), but I can’t seem to hit on the correct syntax. Help?