26

I am writing a cycle method for a list that moves an index either forwards or backwards. The following code is used to cycle backwards:

(i-1)%list_length

In this case, i is of the type usize, meaning it is unsigned. If i is equal to 0, this leads to an 'attempt to subtract with overflow' error. I tried to use the correct casting methods to work around this problem:

((i as isize)-1)%(list_length as isize)) as usize

This results in an integer overflow.

I understand why the errors happen, and at the moment I've solved the problem by checking if the index is equal to 0, but I was wondering if there was some way to solve it by casting the variables to the correct types.

Shepmaster
  • 274,917
  • 47
  • 731
  • 969
lmartens
  • 1,272
  • 2
  • 10
  • 18
  • 6
    As an aside: I don't think you want to do that *at all.* `(-1 % 10)` is `-1`, not `9`. `-1isize as usize` is `18446744073709551615` (on 64-bit architectures). – DK. Dec 23 '16 at 12:33
  • Ok, I didn't know. I thought it worked like described in [this post](http://math.stackexchange.com/questions/519845/modulo-of-a-negative-number), but I see now that it is implemented like described in [this post](http://stackoverflow.com/questions/31210357/is-there-a-modulus-not-remainder-function-operation). That clears it up! – lmartens Dec 23 '16 at 14:03

2 Answers2

17

As DK. points out, you don't want wrapping semantics at the integer level:

fn main() {
    let idx: usize = 0;
    let len = 10;

    let next_idx = idx.wrapping_sub(1) % len;
    println!("{}", next_idx) // Prints 5!!!
}

Instead, you want to use modulo logic to wrap around:

let next_idx = (idx + len - 1) % len;

This only works if len + idx is less than the max of the type — this is much easier to see with a u8 instead of usize; just set idx to 200 and len to 250.

If you can't guarantee that the sum of the two values will always be less than the maximum value, I'd probably use the "checked" family of operations. This does the same level of conditional checking you mentioned you already have, but is neatly tied into a single line:

let next_idx = idx.checked_sub(1).unwrap_or(len - 1);
Community
  • 1
  • 1
Shepmaster
  • 274,917
  • 47
  • 731
  • 969
7

If your code can have overflowing operations, I would suggest using Wrapping. You don't need to worry about casting or overflow panics when you allow it:

use std::num::Wrapping;

let zero = Wrapping(0u32);
let one = Wrapping(1u32);

assert_eq!(std::u32::MAX, (zero - one).0);
Shepmaster
  • 274,917
  • 47
  • 731
  • 969
ljedrz
  • 16,004
  • 3
  • 53
  • 74
  • 5
    There's also [inherent methods on each type](https://doc.rust-lang.org/std/primitive.usize.html#method.wrapping_sub) for wrapping arithmetic. – Shepmaster Dec 23 '16 at 14:06