Last active
January 28, 2023 15:45
-
-
Save kriogenia/e54626364bf2e9aa558cd5b1444d9f0e to your computer and use it in GitHub Desktop.
Behavior of Integer Arithmetics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
// Result of the different operations computing 255 + 1 (overflowing the u8) | |
assert_eq!(u8::MAX.wrapping_add(1), 0); | |
assert_eq!(u8::MAX.checked_add(1), None); | |
assert_eq!(u8::MAX.overflowing_add(1), (0, true)); | |
assert_eq!(u8::MAX.saturating_add(1), u8::MAX); | |
// Wrapping examples | |
assert_eq!(i32::MAX.wrapping_add(12), i32::MIN + 11); | |
assert_eq!(0u8.wrapping_sub(1), u8::MAX); | |
// Checked examples | |
assert_eq!(i32::MAX.checked_pow(2), None); | |
assert_eq!(1u8.checked_mul(5), Some(5)); | |
// Overflowing examples | |
assert_eq!(i32::MIN.overflowing_abs(), (i32::MIN, true)); | |
assert_eq!(4i32.overflowing_div(2), (2, false)); | |
// Saturating examples | |
assert_eq!(1u8.saturating_sub(3), 0); | |
assert_eq!(2i32.saturating_neg(), -2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment