diff options
| author | Jonathan Turner <jonathandturner@users.noreply.github.com> | 2016-08-22 15:34:19 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-08-22 15:34:19 -0700 |
| commit | b6950492c2cea96d0d8a18f9d024e80cf0990f2d (patch) | |
| tree | f36a24113dc70ad5f403ffd73d3b29baf3c43767 /src/libcore | |
| parent | f46438c71af4547916dde0a24f8cd4241a49265e (diff) | |
| parent | 9f88f8ae4831a6f78781dd7c06181c8f3d21ad75 (diff) | |
| download | rust-b6950492c2cea96d0d8a18f9d024e80cf0990f2d.tar.gz rust-b6950492c2cea96d0d8a18f9d024e80cf0990f2d.zip | |
Rollup merge of #35809 - matthew-piziak:bitwise-and-example, r=GuillaumeGomez
replace `BitAnd` example with something more evocative of bitwise AND
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ops.rs | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 3a2d6c8bcf7..c2540d8486c 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -694,26 +694,41 @@ not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 } /// /// # Examples /// -/// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up -/// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`. +/// In this example, the `BitAnd` trait is implemented for a `BooleanVector` +/// struct. /// /// ``` /// use std::ops::BitAnd; /// -/// struct Foo; -/// -/// impl BitAnd for Foo { -/// type Output = Foo; -/// -/// fn bitand(self, _rhs: Foo) -> Foo { -/// println!("Bitwise And-ing!"); -/// self +/// #[derive(Debug)] +/// struct BooleanVector { +/// value: Vec<bool>, +/// }; +/// +/// impl BitAnd for BooleanVector { +/// type Output = Self; +/// +/// fn bitand(self, rhs: Self) -> Self { +/// BooleanVector { +/// value: self.value +/// .iter() +/// .zip(rhs.value.iter()) +/// .map(|(x, y)| *x && *y) +/// .collect(), +/// } /// } /// } /// -/// fn main() { -/// Foo & Foo; +/// impl PartialEq for BooleanVector { +/// fn eq(&self, other: &Self) -> bool { +/// self.value == other.value +/// } /// } +/// +/// let bv1 = BooleanVector { value: vec![true, true, false, false] }; +/// let bv2 = BooleanVector { value: vec![true, false, true, false] }; +/// let expected = BooleanVector { value: vec![true, false, false, false] }; +/// assert_eq!(bv1 & bv2, expected); /// ``` #[lang = "bitand"] #[stable(feature = "rust1", since = "1.0.0")] |
