I was writing some code where I want to use an iterator, or its reversed version depending on a flag, but the straightforward code gives an error
pub fn eggs<I,T>(iter:I)->Box<dyn Iterator<Item=T>> where I:Iterator<Item=T>+DoubleEndedIterator { Box::new(iter.rev()) } pub fn bacon<I,T>(iter:I, reverse:bool) -> Box<dyn Iterator<Item=T>> where I:Iterator<Item=T>+DoubleEndedIterator { if reverse { Box::new(iter.rev()) } else { Box::new(iter) } } fn main() { let pants:String = "pants".into(); eggs(pants.chars()); } fails to compile:
error[E0310]: the parameter type `I` may not live long enough --> src/main.rs:5:5 | 2 | pub fn eggs<I,T>(iter:I)->Box<dyn Iterator<Item=T>> | - help: consider adding an explicit lifetime bound...: `I: 'static` ... 5 | Box::new(iter.rev()) | ^^^^^^^^^^^^^^^^^^^^ ...so that the type `Rev<I>` will meet its required lifetime bounds With my limited understanding of Rust, I'm not sure where those lifetime bounds are coming from. There aren't any on the Iterator trait, or the Rev struct, and the parameter is being moved.
What is the proper way to declare these sorts of functions given that 'static isn't really an option.