pub trait SafeRem: Copy + Rem<Output = Self> {
// Required method
fn safe_rem(self, rhs: Self) -> Result<Self, SafeMathError>;
}Expand description
Safe remainder operation with division-by-zero checking.
This trait provides checked remainder (modulo) operations that return a Result
instead of panicking when attempting to compute remainder with a zero divisor.
The operation follows the same rules as Rust’s % operator for the sign of the result.
§Arguments
rhs- Right-hand side operand (divisor).
§Returns
Ok(result)- The remainder ofselfdivided byrhsif operation is validErr(SafeMathError::DivisionByZero)- Ifrhsis zero
§Examples
use safe_math::{SafeRem, SafeMathError};
let a: i8 = 10;
let b: i8 = 3;
// Safe remainder that works
assert_eq!(a.safe_rem(b), Ok(1)); // 10 % 3 = 1
// Safe remainder that detects division by zero
let zero: i8 = 0;
assert_eq!(a.safe_rem(zero), Err(SafeMathError::DivisionByZero));
// Example with negative numbers
let neg_a: i8 = -10;
assert_eq!(neg_a.safe_rem(b), Ok(-1)); // -10 % 3 = -1§See also
SafeMathOps- Combined trait for all safe arithmetic operationsSafeMathError- Error type returned on arithmetic failuresSafeDiv- Safe division operations
Required Methods§
Sourcefn safe_rem(self, rhs: Self) -> Result<Self, SafeMathError>
fn safe_rem(self, rhs: Self) -> Result<Self, SafeMathError>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.