我有两个u64
,我想将它们除以结果。 (由于这些是无符号的,因此向零的下限和舍入是相同的。)
我怎样才能做到这一点?
最佳答案
impl Div<i32> for i32文档说:
它适用于所有整数类型:
macro_rules! div_impl_integer {
($($t:ty)*) => ($(
/// This operation rounds towards zero, truncating any
/// fractional part of the exact result.
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
type Output = $t;
#[inline]
fn div(self, other: $t) -> $t { self / other }
}
forward_ref_binop! { impl Div, div for $t, $t }
)*)
}
div_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
因此,您需要做的只是正常划分。
assert_eq!(3/2, 1);
assert_eq!(9/4, 2);