1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/// The function `try_add` in Rust attempts to add two `u128` values and returns the sum as an
/// `Option<u128>` if the result does not overflow.
///
/// Arguments:
///
/// * `left`: `u128` - an unsigned 128-bit integer representing the left operand for addition.
/// * `right`: The `right` parameter in the `try_add` function is of type `u128`, which represents an
/// unsigned 128-bit integer.
///
/// Returns:
///
/// The function `try_add` returns an `Option<u128>`. It returns `Some(add_value)` if the sum of `left`
/// and `right` is greater than both `left` and `right`, otherwise it returns `None`.
pub fn try_add(left: u128, right: u128) -> Option<u128> {
let add_value = left + right;
if add_value > left && add_value > right {
return Some(add_value);
} else {
return None;
}
}
/// The function `try_sub` in Rust attempts to subtract two `u128` values and returns the result if it
/// is greater than the second value, otherwise it returns `None`.
///
/// Arguments:
///
/// * `left`: The `left` parameter represents the value from which you want to subtract another value.
/// * `right`: The `right` parameter is the value that will be subtracted from the `left` parameter in
/// the `try_sub` function.
///
/// Returns:
///
/// The function `try_sub` returns an `Option<u128>`. It returns `None` if the `left` value is less than
/// the `right` value. If the subtraction result is greater than the `right` value, it returns
/// `Some(sub_value)`, where `sub_value` is the result of the subtraction. Otherwise, it returns `None`.
pub fn try_sub(left: u128, right: u128) -> Option<u128> {
if left < right {
return None;
}
let sub_value = left - right;
if sub_value > right {
return Some(sub_value);
} else {
return None;
}
}
/// The function `try_mul` in Rust attempts to multiply two `u128` numbers and returns the result as an
/// `Option<u128>`, ensuring no overflow occurs.
///
/// Arguments:
///
/// * `left`: The `left` parameter is a 128-bit unsigned integer.
/// * `right`: The `right` parameter is a 128-bit unsigned integer value.
///
/// Returns:
///
/// The `try_mul` function returns an `Option<u128>`. It returns `None` if the `left` parameter is equal
/// to 0 or if the multiplication of `left` and `right` would result in an overflow. Otherwise, it
/// returns `Some(mul_value)`, where `mul_value` is the result of the multiplication.
pub fn try_mul(left: u128, right: u128) -> Option<u128> {
if left == 0 {
return None;
}
let mul_value = left * right;
if mul_value / left == right {
return Some(mul_value);
} else {
return None;
}
}
/// The function `try_div` in Rust attempts to divide two `u128` numbers and returns the result as an
/// `Option<u128>`, or `None` if the division is not exact.
///
/// Arguments:
///
/// * `left`: The `left` parameter represents the dividend in a division operation.
/// * `right`: The `right` parameter represents the divisor in the division operation.
///
/// Returns:
///
/// The function `try_div` returns an `Option<u128>`. It returns `None` if the `right` parameter is
/// equal to 0 or if the division does not result in a whole number quotient. It returns
/// `Some(mul_value)` if the division results in a whole number quotient.
pub fn try_div(left: u128, right: u128) -> Option<u128> {
if right == 0 {
return None;
}
let mul_value = left / right;
if mul_value * right == left {
return Some(mul_value);
} else {
return None;
}
}
/// This Rust function calculates the remainder of dividing one unsigned 128-bit integer by another,
/// returning an `Option` with the result or `None` if the divisor is zero.
///
/// Arguments:
///
/// * `left`: The `left` parameter is a 128-bit unsigned integer.
/// * `right`: The `right` parameter is the divisor used in the modulo operation.
///
/// Returns:
///
/// The function `try_mod` returns an `Option<u128>`, which can either be `Some(u128)` if the `right`
/// value is not zero, or `None` if the `right` value is zero.
pub fn try_mod(left: u128, right: u128) -> Option<u128> {
if right == 0 {
return None;
}
let mul_value = left % right;
return Some(mul_value);
}