10

Swift has integer overflow arithmetic functions which return a flag whether the number has overflowed or not. Do we have same thing in Rust?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
eonil
  • 83,476
  • 81
  • 317
  • 516

3 Answers3

17

Since Rust 1.7.0, there's overflowing_<operation>(rhs) defined on integer types.

Example for overflowing_add:

Calculates self + rhs

Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.

Example:

use std::i64;

assert_eq!(5i64.overflowing_add(2), (7, false));
assert_eq!(i64::MAX.overflowing_add(1), (i64::MIN, true));

(playground)

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
15

As you note, there are intrinsics for this but these are unsafe and somewhat annoying to use.

Before Rust 1.0, the standard library provided wrappers that detect the overflow for the 4 arithmetic operations in the form of CheckedAdd, CheckedSub, CheckedMul and CheckedDiv.

As of Rust 1.0, these traits no longer exist and there are just inherent methods on each numeric type, such as i32::checked_add.

However, these just detect overflow and do not return the overflowed-result:

fn main() {
    println!("{:?}", 5u16.checked_add(65530u16));
    println!("{:?}", 6u16.checked_add(65530u16));
}

(playground)

Prints:

Some(65535)
None
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
huon
  • 94,605
  • 21
  • 231
  • 225
4

Rust has integer arithmetic intrinsics such as add_with_overflow.

pub unsafe extern "rust-intrinsic" fn add_with_overflow<T>(
    x: T, 
    y: T
) -> (T, bool)
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
eonil
  • 83,476
  • 81
  • 317
  • 516
  • Do you happen to know why it's `unsafe`? –  Oct 23 '14 at 23:54
  • 1
    @delnan, all compiler intrinsincs are `unsafe` by default (they're an implementation detail that just gets imported via FFI with a custom ABI, and all FFI functions get marked `unsafe`, there is some thoughts to make this controllable/have safe intrinsics not-`unsafe`, but this is low priority). – huon Oct 24 '14 at 00:24