Skip to content

Commit ba69bc8

Browse files
replace BitAndAssign example with something more evocative
This is the augmented-assignment version of PR #35809. r? @GuillaumeGomez improved documentation a la PR #35993
1 parent 43204ff commit ba69bc8

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
@@ -1291,24 +1291,66 @@ rem_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
12911291
///
12921292
/// # Examples
12931293
///
1294-
/// A trivial implementation of `BitAndAssign`. When `Foo &= Foo` happens, it ends up
1295-
/// calling `bitand_assign`, and therefore, `main` prints `Bitwise And-ing!`.
1294+
/// In this example, the `&=` operator is lifted to a trivial `Scalar` type.
12961295
///
12971296
/// ```
12981297
/// use std::ops::BitAndAssign;
12991298
///
1300-
/// struct Foo;
1299+
/// #[derive(Debug, PartialEq)]
1300+
/// struct Scalar(bool);
13011301
///
1302-
/// impl BitAndAssign for Foo {
1303-
/// fn bitand_assign(&mut self, _rhs: Foo) {
1304-
/// println!("Bitwise And-ing!");
1302+
/// impl BitAndAssign for Scalar {
1303+
/// // rhs is the "right-hand side" of the expression `a &= b`
1304+
/// fn bitand_assign(&mut self, rhs: Self) {
1305+
/// *self = Scalar(self.0 & rhs.0)
13051306
/// }
13061307
/// }
13071308
///
1308-
/// # #[allow(unused_assignments)]
13091309
/// fn main() {
1310-
/// let mut foo = Foo;
1311-
/// foo &= Foo;
1310+
/// let mut scalar = Scalar(true);
1311+
/// scalar &= Scalar(true);
1312+
/// assert_eq!(scalar, Scalar(true));
1313+
///
1314+
/// let mut scalar = Scalar(true);
1315+
/// scalar &= Scalar(false);
1316+
/// assert_eq!(scalar, Scalar(false));
1317+
///
1318+
/// let mut scalar = Scalar(false);
1319+
/// scalar &= Scalar(true);
1320+
/// assert_eq!(scalar, Scalar(false));
1321+
///
1322+
/// let mut scalar = Scalar(false);
1323+
/// scalar &= Scalar(false);
1324+
/// assert_eq!(scalar, Scalar(false));
1325+
/// }
1326+
/// ```
1327+
///
1328+
/// In this example, the `BitAndAssign` trait is implemented for a
1329+
/// `BooleanVector` struct.
1330+
///
1331+
/// ```
1332+
/// use std::ops::BitAndAssign;
1333+
///
1334+
/// #[derive(Debug, PartialEq)]
1335+
/// struct BooleanVector(Vec<bool>);
1336+
///
1337+
/// impl BitAndAssign for BooleanVector {
1338+
/// // rhs is the "right-hand side" of the expression `a &= b`
1339+
/// fn bitand_assign(&mut self, rhs: Self) {
1340+
/// assert_eq!(self.0.len(), rhs.0.len());
1341+
/// *self = BooleanVector(self.0
1342+
/// .iter()
1343+
/// .zip(rhs.0.iter())
1344+
/// .map(|(x, y)| *x && *y)
1345+
/// .collect());
1346+
/// }
1347+
/// }
1348+
///
1349+
/// fn main() {
1350+
/// let mut bv = BooleanVector(vec![true, true, false, false]);
1351+
/// bv &= BooleanVector(vec![true, false, true, false]);
1352+
/// let expected = BooleanVector(vec![true, false, false, false]);
1353+
/// assert_eq!(bv, expected);
13121354
/// }
13131355
/// ```
13141356
#[lang = "bitand_assign"]

0 commit comments

Comments
 (0)