@@ -894,25 +894,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
894
894
///
895
895
/// # Examples
896
896
///
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 `.
899
899
///
900
900
/// ```
901
901
/// use std::ops::AddAssign;
902
902
///
903
- /// struct Foo;
903
+ /// #[derive(Debug)]
904
+ /// struct Point {
905
+ /// x: i32,
906
+ /// y: i32,
907
+ /// }
904
908
///
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
+ /// };
908
915
/// }
909
916
/// }
910
917
///
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
+ /// }
915
922
/// }
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 });
916
927
/// ```
917
928
#[ lang = "add_assign" ]
918
929
#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments