/// Performs multiplication that returns `None` instead of wrapping around on underflow or /// overflow. pubtrait CheckedMul: Sized + Mul<Self, Output = Self> { /// Multiplies two numbers, checking for underflow or overflow. If underflow /// or overflow happens, `None` is returned. fn checked_mul(&self, v: &Self) -> Option<Self>;
}
/// Performs division that returns `None` instead of panicking on division by zero and instead of /// wrapping around on underflow and overflow. pubtrait CheckedDiv: Sized + Div<Self, Output = Self> { /// Divides two numbers, checking for underflow, overflow and division by /// zero. If any of that happens, `None` is returned. fn checked_div(&self, v: &Self) -> Option<Self>;
}
/// Performs an integral remainder that returns `None` instead of panicking on division by zero and /// instead of wrapping around on underflow and overflow. pubtrait CheckedRem: Sized + Rem<Self, Output = Self> { /// Finds the remainder of dividing two numbers, checking for underflow, overflow and division /// by zero. If any of that happens, `None` is returned. /// /// # Examples /// /// ``` /// use num_traits::CheckedRem; /// use std::i32::MIN; /// /// assert_eq!(CheckedRem::checked_rem(&10, &7), Some(3)); /// assert_eq!(CheckedRem::checked_rem(&10, &-7), Some(3)); /// assert_eq!(CheckedRem::checked_rem(&-10, &7), Some(-3)); /// assert_eq!(CheckedRem::checked_rem(&-10, &-7), Some(-3)); /// /// assert_eq!(CheckedRem::checked_rem(&10, &0), None); /// /// assert_eq!(CheckedRem::checked_rem(&MIN, &1), Some(0)); /// assert_eq!(CheckedRem::checked_rem(&MIN, &-1), None); /// ``` fn checked_rem(&self, v: &Self) -> Option<Self>;
}
/// Performs negation that returns `None` if the result can't be represented. pubtrait CheckedNeg: Sized { /// Negates a number, returning `None` for results that can't be represented, like signed `MIN` /// values that can't be positive, or non-zero unsigned values that can't be negative. /// /// # Examples /// /// ``` /// use num_traits::CheckedNeg; /// use std::i32::MIN; /// /// assert_eq!(CheckedNeg::checked_neg(&1_i32), Some(-1)); /// assert_eq!(CheckedNeg::checked_neg(&-1_i32), Some(1)); /// assert_eq!(CheckedNeg::checked_neg(&MIN), None); /// /// assert_eq!(CheckedNeg::checked_neg(&0_u32), Some(0)); /// assert_eq!(CheckedNeg::checked_neg(&1_u32), None); /// ``` fn checked_neg(&self) -> Option<Self>;
}
/// Performs a left shift that returns `None` on shifts larger than /// or equal to the type width. pubtrait CheckedShl: Sized + Shl<u32, Output = Self> { /// Checked shift left. Computes `self << rhs`, returning `None` /// if `rhs` is larger than or equal to the number of bits in `self`. /// /// ``` /// use num_traits::CheckedShl; /// /// let x: u16 = 0x0001; /// /// assert_eq!(CheckedShl::checked_shl(&x, 0), Some(0x0001)); /// assert_eq!(CheckedShl::checked_shl(&x, 1), Some(0x0002)); /// assert_eq!(CheckedShl::checked_shl(&x, 15), Some(0x8000)); /// assert_eq!(CheckedShl::checked_shl(&x, 16), None); /// ``` fn checked_shl(&self, rhs: u32) -> Option<Self>;
}
/// Performs a right shift that returns `None` on shifts larger than /// or equal to the type width. pubtrait CheckedShr: Sized + Shr<u32, Output = Self> { /// Checked shift right. Computes `self >> rhs`, returning `None` /// if `rhs` is larger than or equal to the number of bits in `self`. /// /// ``` /// use num_traits::CheckedShr; /// /// let x: u16 = 0x8000; /// /// assert_eq!(CheckedShr::checked_shr(&x, 0), Some(0x8000)); /// assert_eq!(CheckedShr::checked_shr(&x, 1), Some(0x4000)); /// assert_eq!(CheckedShr::checked_shr(&x, 15), Some(0x0001)); /// assert_eq!(CheckedShr::checked_shr(&x, 16), None); /// ``` fn checked_shr(&self, rhs: u32) -> Option<Self>;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.