std::ops::BitOr

Trait std::ops::BitOr

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

The bitwise OR operator |.

Examples

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

use std::ops::BitOr;

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

impl BitOr for Scalar {
    type Output = Self;

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

fn main() {
    assert_eq!(Scalar(true) | Scalar(true), Scalar(true));
    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 BitOr trait is implemented for a BooleanVector struct.

use std::ops::BitOr;

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

impl BitOr for BooleanVector {
    type Output = Self;

    fn bitor(self, BooleanVector(rhs): Self) -> Self {
        let BooleanVector(lhs) = self;
        assert_eq!(lhs.len(), rhs.len());
        BooleanVector(lhs.iter().zip(rhs.iter()).map(|(x, y)| *x || *y).collect())
    }
}

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

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.BitOr.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部