pub trait ShlAssign<Rhs = Self> {
// Required method
fn shl_assign(&mut self, rhs: Rhs);
}
Expand description
左移赋值运算符 <<=
。
ShlAssign
的实现,用于围绕 usize
的包装。
use std::ops::ShlAssign;
#[derive(Debug, PartialEq)]
struct Scalar(usize);
impl ShlAssign<usize> for Scalar {
fn shl_assign(&mut self, rhs: usize) {
self.0 <<= rhs;
}
}
let mut scalar = Scalar(4);
scalar <<= 2;
assert_eq!(scalar, Scalar(16));
Run
执行 <<=
操作。
let mut x: u8 = 5;
x <<= 1;
assert_eq!(x, 10);
let mut x: u8 = 1;
x <<= 1;
assert_eq!(x, 2);
Run