I have a slice that I want to walk over in reverse and need to know the index position. I initially tried:
for (i, register) in registers.iter().rev().enumerate() { ... }
but I quickly discovered that while the slice is processed in reverse, the index still counts up from zero. I thought the rev carried through to chained calls, but that was incorrect. Fortunately, a test caught it.
I could just treat i as an offset from the end and subtract my way through, but that sort of thing often leads to off-by-one errors. So, I opted to find a way to have i be the index (implicitly offset from 0). Currently, I'm doing:
for i in (0..registers.len()).rev() {
let register = ®isters[i];
...
}
It's not the ugliest thing in the world, but it lacks some of the elegance of my first approach. Is there a more idiomatic way to enumerate a slice in reverse and get the index of the current element?