BitAndAssign

Trait BitAndAssign 

1.8.0 (const: unstable) · Source
pub trait BitAndAssign<Rhs = Self> {
    // Required method
    fn bitand_assign(&mut self, rhs: Rhs);
}
Expand description

The bitwise AND assignment operator &=.

§Examples

An implementation of BitAndAssign that lifts the &= operator to a wrapper around bool.

use std::ops::BitAndAssign;

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

impl BitAndAssign for Scalar {
    // rhs is the "right-hand side" of the expression `a &= b`
    fn bitand_assign(&mut self, rhs: Self) {
        *self = Self(self.0 & rhs.0)
    }
}

let mut scalar = Scalar(true);
scalar &= Scalar(true);
assert_eq!(scalar, Scalar(true));

let mut scalar = Scalar(true);
scalar &= Scalar(false);
assert_eq!(scalar, Scalar(false));

let mut scalar = Scalar(false);
scalar &= Scalar(true);
assert_eq!(scalar, Scalar(false));

let mut scalar = Scalar(false);
scalar &= Scalar(false);
assert_eq!(scalar, Scalar(false));

Here, the BitAndAssign trait is implemented for a wrapper around Vec<bool>.

use std::ops::BitAndAssign;

#[derive(Debug, PartialEq)]
struct BooleanVector(Vec<bool>);

impl BitAndAssign for BooleanVector {
    // `rhs` is the "right-hand side" of the expression `a &= b`.
    fn bitand_assign(&mut self, rhs: Self) {
        assert_eq!(self.0.len(), rhs.0.len());
        *self = Self(
            self.0
                .iter()
                .zip(rhs.0.iter())
                .map(|(x, y)| *x & *y)
                .collect()
        );
    }
}

let mut bv = BooleanVector(vec![true, true, false, false]);
bv &= BooleanVector(vec![true, false, true, false]);
let expected = BooleanVector(vec![true, false, false, false]);
assert_eq!(bv, expected);

Required Methods§

1.8.0 · Source

fn bitand_assign(&mut self, rhs: Rhs)

Performs the &= operation.

§Examples
let mut x = true;
x &= false;
assert_eq!(x, false);

let mut x = true;
x &= true;
assert_eq!(x, true);

let mut x: u8 = 5;
x &= 1;
assert_eq!(x, 1);

let mut x: u8 = 5;
x &= 2;
assert_eq!(x, 0);

Implementors§

1.8.0 (const: unstable) · Source§

impl BitAndAssign for bool

1.8.0 (const: unstable) · Source§

impl BitAndAssign for i8

1.8.0 (const: unstable) · Source§

impl BitAndAssign for i16

1.8.0 (const: unstable) · Source§

impl BitAndAssign for i32

1.8.0 (const: unstable) · Source§

impl BitAndAssign for i64

1.8.0 (const: unstable) · Source§

impl BitAndAssign for i128

1.8.0 (const: unstable) · Source§

impl BitAndAssign for isize

1.8.0 (const: unstable) · Source§

impl BitAndAssign for u8

1.8.0 (const: unstable) · Source§

impl BitAndAssign for u16

1.8.0 (const: unstable) · Source§

impl BitAndAssign for u32

1.8.0 (const: unstable) · Source§

impl BitAndAssign for u64

1.8.0 (const: unstable) · Source§

impl BitAndAssign for u128

1.8.0 (const: unstable) · Source§

impl BitAndAssign for usize

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&bool> for bool

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&i8> for i8

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&i16> for i16

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&i32> for i32

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&i64> for i64

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&i128> for i128

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&isize> for isize

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&u8> for u8

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&u16> for u16

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&u32> for u32

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&u64> for u64

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&u128> for u128

1.22.0 (const: unstable) · Source§

impl BitAndAssign<&usize> for usize