An iterator and a reverse iterator are completely different types and cannot be stored in the same vector directly. They most likely aren't even the same size.
However, they both implement Iterator, of course. So you can store them via indirection, either as trait object references (&dyn Iterator) or as boxed trait objects (Box<dyn Iterator>). Which one depends on your usecase; the first one is with less overhead, but borrowed; the second one has a minimal overhead, but owns the objects.
In your case, as you don't keep the iterator objects around and instead want to store them in the list directly, the proper solution would be to use Box<dyn Iterator>, like this:
fn main() { let mut output: Vec<Box<dyn Iterator<Item = &u8>>> = Vec::new(); let input = b"1234567890"; let cuts = [(0, 1), (1, 3), (3, 5)]; for (start, end) in cuts { output.push(Box::new(input[start..end].iter())); output.push(Box::new(input[start..end].iter().rev())); } for iter in output { println!("{:?}", iter.collect::<Vec<_>>()); } }
[49] [49] [50, 51] [51, 50] [52, 53] [53, 52]
Minor nitpick:
Iterators over &u8 are discouraged, because &u8 are actually larger than u8. As u8 are Copy, adding copied() to the iterator reduces the item size without any cost; most likely even with a performance benefit.
Reason is that returning a &u8 is slower than a u8 (because &u8 is either 4 or 8 byte, while u8 is a single byte). Further, accessing a &u8 has one indirection, while accessing a u8 is very fast.
So I'd rewrite your code like this:
fn main() { let mut output: Vec<Box<dyn Iterator<Item = u8>>> = Vec::new(); let input = b"1234567890"; let cuts = [(0, 1), (1, 3), (3, 5)]; for (start, end) in cuts { output.push(Box::new(input[start..end].iter().copied())); output.push(Box::new(input[start..end].iter().copied().rev())); } for iter in output { println!("{:?}", iter.collect::<Vec<_>>()); } }
[49] [49] [50, 51] [51, 50] [52, 53] [53, 52]
Of course this is only true for &u8 and not for &mut u8; if you want to mutate the original items, copying them is counterproductive.