Skip to content

Commit 6c66eaa

Browse files
replace AddAssign example with something more evocative of addition
This is analogous to PR #35709 for the `Add` trait.
1 parent 11f8805 commit 6c66eaa

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/libcore/ops.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -894,25 +894,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
894894
///
895895
/// # Examples
896896
///
897-
/// A trivial implementation of `AddAssign`. When `Foo += Foo` happens, it ends up
898-
/// calling `add_assign`, and therefore, `main` prints `Adding!`.
897+
/// This example creates a `Point` struct that implements the `AddAssign`
898+
/// trait, and then demonstrates add-assigning to a mutable `Point`.
899899
///
900900
/// ```
901901
/// use std::ops::AddAssign;
902902
///
903-
/// struct Foo;
903+
/// #[derive(Debug)]
904+
/// struct Point {
905+
/// x: i32,
906+
/// y: i32,
907+
/// }
904908
///
905-
/// impl AddAssign for Foo {
906-
/// fn add_assign(&mut self, _rhs: Foo) {
907-
/// println!("Adding!");
909+
/// impl AddAssign for Point {
910+
/// fn add_assign(&mut self, other: Point) {
911+
/// *self = Point {
912+
/// x: self.x + other.x,
913+
/// y: self.y + other.y,
914+
/// };
908915
/// }
909916
/// }
910917
///
911-
/// # #[allow(unused_assignments)]
912-
/// fn main() {
913-
/// let mut foo = Foo;
914-
/// foo += Foo;
918+
/// impl PartialEq for Point {
919+
/// fn eq(&self, other: &Self) -> bool {
920+
/// self.x == other.x && self.y == other.y
921+
/// }
915922
/// }
923+
///
924+
/// let mut point = Point { x: 1, y: 0 };
925+
/// point += Point { x: 2, y: 3 };
926+
/// assert_eq!(point, Point { x: 3, y: 3 });
916927
/// ```
917928
#[lang = "add_assign"]
918929
#[stable(feature = "op_assign_traits", since = "1.8.0")]

0 commit comments

Comments
 (0)