std::ops::Shl

Trait std::ops::Shl

#[lang = "shl"]
pub trait Shl<RHS> {
    type Output;
    fn shl(self, rhs: RHS) -> Self::Output;
}

The left shift operator <<.

Examples

An implementation of Shl that lifts the << operation on integers to a Scalar struct.

use std::ops::Shl;

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

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

    fn shl(self, Scalar(rhs): Self) -> Scalar {
        let Scalar(lhs) = self;
        Scalar(lhs << rhs)
    }
}
fn main() {
    assert_eq!(Scalar(4) << Scalar(2), Scalar(16));
}

An implementation of Shl that spins a vector leftward by a given amount.

use std::ops::Shl;

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

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

    fn shl(self, rhs: usize) -> SpinVector<T> {
        // rotate the vector by `rhs` places
        let (a, b) = self.vec.split_at(rhs);
        let mut spun_vector: Vec<T> = vec![];
        spun_vector.extend_from_slice(b);
        spun_vector.extend_from_slice(a);
        SpinVector { vec: spun_vector }
    }
}

fn main() {
    assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
               SpinVector { vec: vec![2, 3, 4, 0, 1] });
}

Associated Types

The resulting type after applying the << operator

Required Methods

The method for the << operator

Implementors

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/ops/trait.Shl.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部