What you are doing in that case is pointer arithmetic, and imo it would be better to do that with pointers. And pointers have an offset method that accept isize.
I don’t really like this notion. It’s already easy to do using unsafe, so let’s not add a safe API equivalent?
This is definitely something I have wished for in the past. Just a few days ago I wanted some code to me generic over array iteration direction (and with logic more complicated than easily / cleanly fits in an iterator), so I wrote something along these lines:
trait Direction {
fn modify(x: &mut usize);
}
struct Forward;
struct Backward;
impl Direction for Forward { fn modify(x: &mut usize) { *x += 1 } }
impl Direction for Backward { fn modify(x: &mut usize) { *x -= 1 } }
fn complicated_scan<T, D: Direction>(array: &[T], mut i: usize) {
while condition(i) {
D::modify(i);
}
}
Even taking the direction as a 1 or -1isize and casting the index back and forth would have been easier.