Skip to content

Commit b695049

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35809 - matthew-piziak:bitwise-and-example, r=GuillaumeGomez
replace `BitAnd` example with something more evocative of bitwise AND
2 parents f46438c + 9f88f8a commit b695049

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/libcore/ops.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -694,26 +694,41 @@ not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
694694
///
695695
/// # Examples
696696
///
697-
/// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
698-
/// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
697+
/// In this example, the `BitAnd` trait is implemented for a `BooleanVector`
698+
/// struct.
699699
///
700700
/// ```
701701
/// use std::ops::BitAnd;
702702
///
703-
/// struct Foo;
704-
///
705-
/// impl BitAnd for Foo {
706-
/// type Output = Foo;
707-
///
708-
/// fn bitand(self, _rhs: Foo) -> Foo {
709-
/// println!("Bitwise And-ing!");
710-
/// self
703+
/// #[derive(Debug)]
704+
/// struct BooleanVector {
705+
/// value: Vec<bool>,
706+
/// };
707+
///
708+
/// impl BitAnd for BooleanVector {
709+
/// type Output = Self;
710+
///
711+
/// fn bitand(self, rhs: Self) -> Self {
712+
/// BooleanVector {
713+
/// value: self.value
714+
/// .iter()
715+
/// .zip(rhs.value.iter())
716+
/// .map(|(x, y)| *x && *y)
717+
/// .collect(),
718+
/// }
711719
/// }
712720
/// }
713721
///
714-
/// fn main() {
715-
/// Foo & Foo;
722+
/// impl PartialEq for BooleanVector {
723+
/// fn eq(&self, other: &Self) -> bool {
724+
/// self.value == other.value
725+
/// }
716726
/// }
727+
///
728+
/// let bv1 = BooleanVector { value: vec![true, true, false, false] };
729+
/// let bv2 = BooleanVector { value: vec![true, false, true, false] };
730+
/// let expected = BooleanVector { value: vec![true, false, false, false] };
731+
/// assert_eq!(bv1 & bv2, expected);
717732
/// ```
718733
#[lang = "bitand"]
719734
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)