用法
pub trait BitXor<Rhs = Self> {
type Output;
fn bitxor(self, rhs:Rhs) -> Self::Output;
}
按位异或运算符 ^
。
请注意,Rhs
默认为 Self
,但这不是强制性的。
例子
BitXor
的实现,将 ^
提升到围绕 bool
的包装器。
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::Output {
Self(self.0 ^ rhs.0)
}
}
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));
BitXor
trait 的实现,用于围绕 Vec<bool>
的包装器。
use std::ops::BitXor;
#[derive(Debug, PartialEq)]
struct BooleanVector(Vec<bool>);
impl BitXor for BooleanVector {
type Output = Self;
fn bitxor(self, Self(rhs):Self) -> Self::Output {
let Self(lhs) = self;
assert_eq!(lhs.len(), rhs.len());
Self(
lhs.iter()
.zip(rhs.iter())
.map(|(x, y)| *x ^ *y)
.collect()
)
}
}
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);
相关用法
- Rust std::ops::BitXorAssign用法及代码示例
- Rust std::ops::BitXorAssign.bitxor_assign用法及代码示例
- Rust std::ops::BitAndAssign用法及代码示例
- Rust std::ops::BitOrAssign用法及代码示例
- Rust std::ops::BitAnd用法及代码示例
- Rust std::ops::BitOrAssign.bitor_assign用法及代码示例
- Rust std::ops::BitAndAssign.bitand_assign用法及代码示例
- Rust std::ops::BitAnd.bitand用法及代码示例
- Rust std::ops::BitOr用法及代码示例
- Rust std::ops::Bound.cloned用法及代码示例
- Rust std::ops::Bound.map用法及代码示例
- Rust std::ops::Bound用法及代码示例
- Rust std::ops::Shl用法及代码示例
- Rust std::ops::RangeInclusive.end用法及代码示例
- Rust std::ops::DivAssign用法及代码示例
- Rust std::ops::Try.from_output用法及代码示例
注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 Trait std::ops::BitXor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。