Skip to content

Commit 9278b4c

Browse files
committed
Document panicking cases for integer division
The panic on division by zero is expected, but the panic on overflow is somewhat surprising (since most arithmetic operations panic on overflow only when `debug_assertions` is enabled). As a result, it's helpful to document this behavior.
1 parent 5233edc commit 9278b4c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

library/core/src/ops/arith.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,13 @@ pub trait Div<Rhs = Self> {
456456
}
457457

458458
macro_rules! div_impl_integer {
459-
($($t:ty)*) => ($(
459+
($(($($t:ty)*) => $panic:expr),*) => ($($(
460460
/// This operation rounds towards zero, truncating any
461461
/// fractional part of the exact result.
462+
///
463+
/// # Panics
464+
///
465+
#[doc = $panic]
462466
#[stable(feature = "rust1", since = "1.0.0")]
463467
impl Div for $t {
464468
type Output = $t;
@@ -468,10 +472,13 @@ macro_rules! div_impl_integer {
468472
}
469473

470474
forward_ref_binop! { impl Div, div for $t, $t }
471-
)*)
475+
)*)*)
472476
}
473477

474-
div_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
478+
div_impl_integer! {
479+
(usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
480+
(isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or the division results in overflow."
481+
}
475482

476483
macro_rules! div_impl_float {
477484
($($t:ty)*) => ($(

0 commit comments

Comments
 (0)