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

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

右移运算符 >>。 请注意,因为此 trait 是针对具有多个右侧类型的所有整数类型实现的,所以 Rust 的类型检查器对 _ >> _ 具有特殊的处理方式,将整数运算的结果类型设置为左侧操作数的类型。

这意味着尽管从评估的角度来看,a >> ba.shr(b) 是相同的,但是在类型推断方面它们是不同的。

Examples

Shr 的实现,将整数的 >> 操作提升为 usize 的包装器。

use std::ops::Shr;

#[derive(PartialEq, Debug)]
struct Scalar(usize);

impl Shr<Scalar> for Scalar {
    type Output = Self;

    fn shr(self, Self(rhs): Self) -> Self::Output {
        let Self(lhs) = self;
        Self(lhs >> rhs)
    }
}

assert_eq!(Scalar(16) >> Scalar(2), Scalar(4));
Run

Shr 的实现,将 vector 向右旋转给定的数量。

use std::ops::Shr;

#[derive(PartialEq, Debug)]
struct SpinVector<T: Clone> {
    vec: Vec<T>,
}

impl<T: Clone> Shr<usize> for SpinVector<T> {
    type Output = Self;

    fn shr(self, rhs: usize) -> Self::Output {
        // 将 vector 旋转 `rhs` 个位置。
        let (a, b) = self.vec.split_at(self.vec.len() - rhs);
        let mut spun_vector = vec![];
        spun_vector.extend_from_slice(b);
        spun_vector.extend_from_slice(a);
        Self { vec: spun_vector }
    }
}

assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } >> 2,
           SpinVector { vec: vec![3, 4, 0, 1, 2] });
Run

Associated Types

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

Required methods

执行 >> 操作。

Examples

assert_eq!(5u8 >> 1, 2);
assert_eq!(2u8 >> 1, 1);
Run

Implementors