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

pub trait BitXor<RHS = Self> {
    type Output;
    fn bitxor(self, rhs: RHS) -> Self::Output;

The BitXor trait is used to specify the functionality of ^.


In this example, the ^ operator is lifted to a trivial Scalar type.

use std::ops::BitXor;

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

impl BitXor for Scalar {
    type Output = Self;

    // rhs is the "right-hand side" of the expression `a ^ b`
    fn bitxor(self, rhs: Self) -> Self {
        Scalar(self.0 ^ rhs.0)

fn main() {
    assert_eq!(Scalar(true) ^ Scalar(true), Scalar(false));
    assert_eq!(Scalar(true) ^ Scalar(false), Scalar(true));
    assert_eq!(Scalar(false) ^ Scalar(true), Scalar(true));
    assert_eq!(Scalar(false) ^ Scalar(false), Scalar(false));

In this example, the BitXor trait is implemented for a BooleanVector struct.

use std::ops::BitXor;

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

impl BitXor for BooleanVector {
    type Output = Self;

    fn bitxor(self, BooleanVector(rhs): Self) -> Self {
        let BooleanVector(lhs) = self;
        assert_eq!(lhs.len(), rhs.len());
                         .map(|(x, y)| (*x || *y) && !(*x && *y))

fn main() {
    let bv1 = BooleanVector(vec![true, true, false, false]);
    let bv2 = BooleanVector(vec![true, false, true, false]);
    let expected = BooleanVector(vec![false, true, true, false]);
    assert_eq!(bv1 ^ bv2, expected);

Associated Types

The resulting type after applying the ^ operator

Required Methods

The method for the ^ operator