Logo

Programming-Idioms

History of Idiom 86 > diff from v17 to v18

Edit summary for version 18 by Debaran:
New Rust implementation by user [Debaran]

Version 17

2018-03-05, 16:05:21

Version 18

2019-02-01, 05:08:43

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Code
fn multiply_will_overflow(x: i64, y: i64) -> bool {
    x.checked_mul(y).is_none()
}
Comments bubble
checked_mul is available on all integer types
Doc URL
https://doc.rust-lang.org/std/primitive.i64.html#method.checked_mul