Skip to content

Commit bfe5129

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35927 - matthew-piziak:bitandassign-example, r=GuillaumeGomez
replace `BitAndAssign` example with something more evocative This is the augmented-assignment version of PR rust-lang#35809. r? @GuillaumeGomez
2 parents 117cbb8 + ba69bc8 commit bfe5129

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

src/libcore/ops.rs

+51-9
Original file line numberDiff line numberDiff line change
@@ -1564,24 +1564,66 @@ rem_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
15641564
///
15651565
/// # Examples
15661566
///
1567-
/// A trivial implementation of `BitAndAssign`. When `Foo &= Foo` happens, it ends up
1568-
/// calling `bitand_assign`, and therefore, `main` prints `Bitwise And-ing!`.
1567+
/// In this example, the `&=` operator is lifted to a trivial `Scalar` type.
15691568
///
15701569
/// ```
15711570
/// use std::ops::BitAndAssign;
15721571
///
1573-
/// struct Foo;
1572+
/// #[derive(Debug, PartialEq)]
1573+
/// struct Scalar(bool);
15741574
///
1575-
/// impl BitAndAssign for Foo {
1576-
/// fn bitand_assign(&mut self, _rhs: Foo) {
1577-
/// println!("Bitwise And-ing!");
1575+
/// impl BitAndAssign for Scalar {
1576+
/// // rhs is the "right-hand side" of the expression `a &= b`
1577+
/// fn bitand_assign(&mut self, rhs: Self) {
1578+
/// *self = Scalar(self.0 & rhs.0)
15781579
/// }
15791580
/// }
15801581
///
1581-
/// # #[allow(unused_assignments)]
15821582
/// fn main() {
1583-
/// let mut foo = Foo;
1584-
/// foo &= Foo;
1583+
/// let mut scalar = Scalar(true);
1584+
/// scalar &= Scalar(true);
1585+
/// assert_eq!(scalar, Scalar(true));
1586+
///
1587+
/// let mut scalar = Scalar(true);
1588+
/// scalar &= Scalar(false);
1589+
/// assert_eq!(scalar, Scalar(false));
1590+
///
1591+
/// let mut scalar = Scalar(false);
1592+
/// scalar &= Scalar(true);
1593+
/// assert_eq!(scalar, Scalar(false));
1594+
///
1595+
/// let mut scalar = Scalar(false);
1596+
/// scalar &= Scalar(false);
1597+
/// assert_eq!(scalar, Scalar(false));
1598+
/// }
1599+
/// ```
1600+
///
1601+
/// In this example, the `BitAndAssign` trait is implemented for a
1602+
/// `BooleanVector` struct.
1603+
///
1604+
/// ```
1605+
/// use std::ops::BitAndAssign;
1606+
///
1607+
/// #[derive(Debug, PartialEq)]
1608+
/// struct BooleanVector(Vec<bool>);
1609+
///
1610+
/// impl BitAndAssign for BooleanVector {
1611+
/// // rhs is the "right-hand side" of the expression `a &= b`
1612+
/// fn bitand_assign(&mut self, rhs: Self) {
1613+
/// assert_eq!(self.0.len(), rhs.0.len());
1614+
/// *self = BooleanVector(self.0
1615+
/// .iter()
1616+
/// .zip(rhs.0.iter())
1617+
/// .map(|(x, y)| *x && *y)
1618+
/// .collect());
1619+
/// }
1620+
/// }
1621+
///
1622+
/// fn main() {
1623+
/// let mut bv = BooleanVector(vec![true, true, false, false]);
1624+
/// bv &= BooleanVector(vec![true, false, true, false]);
1625+
/// let expected = BooleanVector(vec![true, false, false, false]);
1626+
/// assert_eq!(bv, expected);
15851627
/// }
15861628
/// ```
15871629
#[lang = "bitand_assign"]

0 commit comments

Comments
 (0)