提交 b4585503 编写于 作者: J Jim Turner

Document panicking cases for integer remainder

The panic when the right operand is `0` is expected, but the
overflow-related panic (which occurs only for `MIN % -1`) is somewhat
surprising for two reasons: a return value of `0` would be reasonable
in this case, and, for most arithmetic operations, overflow results in
panic only when `debug_assertions` is enabled. As a result, it's
helpful to document this behavior.
上级 9278b4c6
......@@ -556,9 +556,13 @@ pub trait Rem<Rhs = Self> {
}
macro_rules! rem_impl_integer {
($($t:ty)*) => ($(
($(($($t:ty)*) => $panic:expr),*) => ($($(
/// This operation satisfies `n % d == n - (n / d) * d`. The
/// result has the same sign as the left operand.
///
/// # Panics
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Rem for $t {
type Output = $t;
......@@ -568,10 +572,13 @@ fn rem(self, other: $t) -> $t { self % other }
}
forward_ref_binop! { impl Rem, rem for $t, $t }
)*)
)*)*)
}
rem_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
rem_impl_integer! {
(usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
(isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or if `self / other` results in overflow."
}
macro_rules! rem_impl_float {
($($t:ty)*) => ($(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册