Trait std::ops::Rem1.0.0[][src]

pub trait Rem<Rhs = Self> {
    type Output;
    #[must_use]
    fn rem(self, rhs: Rhs) -> Self::Output;
}
Expand description

余数运算符 %

请注意,默认情况下 RhsSelf,但这不是强制性的。

Examples

本示例在 SplitSlice 对象上实现 Rem。 实现 Rem 后,可以使用 % 运算符将其拆分为给定长度的相等切片后,找出切片的其余元素。

use std::ops::Rem;

#[derive(PartialEq, Debug)]
struct SplitSlice<'a, T: 'a> {
    slice: &'a [T],
}

impl<'a, T> Rem<usize> for SplitSlice<'a, T> {
    type Output = Self;

    fn rem(self, modulus: usize) -> Self::Output {
        let len = self.slice.len();
        let rem = len % modulus;
        let start = len - rem;
        Self {slice: &self.slice[start..]}
    }
}

// 如果将 &[0,1、2、3、4、5、6、7] 分成大小为 3 的切片,则其余部分将为 &[6,7]。
assert_eq!(SplitSlice { slice: &[0, 1, 2, 3, 4, 5, 6, 7] } % 3,
           SplitSlice { slice: &[6, 7] });
Run

Associated Types

应用 % 运算符后的结果类型。

Required methods

执行 % 操作。

Example

assert_eq!(12 % 10, 2);
Run

Implementors

其余部分来自两个彩车的划分。

余数与除数具有相同的符号,并计算为: x - (x / y).trunc() * y.

Examples

let x: f32 = 50.50;
let y: f32 = 8.125;
let remainder = x - (x / y).trunc() * y;

// 两种操作的答案是 1.75
assert_eq!(x % y, remainder);
Run

其余部分来自两个彩车的划分。

余数与除数具有相同的符号,并计算为: x - (x / y).trunc() * y.

Examples

let x: f32 = 50.50;
let y: f32 = 8.125;
let remainder = x - (x / y).trunc() * y;

// 两种操作的答案是 1.75
assert_eq!(x % y, remainder);
Run

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0 or if self / other results in overflow.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d。 结果具有与左操作数相同的符号。

Panics

This operation will panic if other == 0.

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

此操作满足 n % d == n - (n / d) * d,但不能为 panic。

此操作满足 n % d == n - (n / d) * d,但不能为 panic。