@@ -980,25 +980,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
980
980
///
981
981
/// # Examples
982
982
///
983
- /// A trivial implementation of `AddAssign`. When `Foo += Foo` happens, it ends up
984
- /// calling `add_assign` , and therefore, `main` prints `Adding! `.
983
+ /// This example creates a `Point` struct that implements the `AddAssign`
984
+ /// trait , and then demonstrates add-assigning to a mutable `Point `.
985
985
///
986
986
/// ```
987
987
/// use std::ops::AddAssign;
988
988
///
989
- /// struct Foo;
989
+ /// #[derive(Debug)]
990
+ /// struct Point {
991
+ /// x: i32,
992
+ /// y: i32,
993
+ /// }
990
994
///
991
- /// impl AddAssign for Foo {
992
- /// fn add_assign(&mut self, _rhs: Foo) {
993
- /// println!("Adding!");
995
+ /// impl AddAssign for Point {
996
+ /// fn add_assign(&mut self, other: Point) {
997
+ /// *self = Point {
998
+ /// x: self.x + other.x,
999
+ /// y: self.y + other.y,
1000
+ /// };
994
1001
/// }
995
1002
/// }
996
1003
///
997
- /// # #[allow(unused_assignments)]
998
- /// fn main() {
999
- /// let mut foo = Foo;
1000
- /// foo += Foo;
1004
+ /// impl PartialEq for Point {
1005
+ /// fn eq(&self, other: &Self) -> bool {
1006
+ /// self.x == other.x && self.y == other.y
1007
+ /// }
1001
1008
/// }
1009
+ ///
1010
+ /// let mut point = Point { x: 1, y: 0 };
1011
+ /// point += Point { x: 2, y: 3 };
1012
+ /// assert_eq!(point, Point { x: 3, y: 3 });
1002
1013
/// ```
1003
1014
#[ lang = "add_assign" ]
1004
1015
#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments