@@ -930,25 +930,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
930
930
///
931
931
/// # Examples
932
932
///
933
- /// A trivial implementation of `AddAssign`. When `Foo += Foo` happens, it ends up
934
- /// calling `add_assign` , and therefore, `main` prints `Adding! `.
933
+ /// This example creates a `Point` struct that implements the `AddAssign`
934
+ /// trait , and then demonstrates add-assigning to a mutable `Point `.
935
935
///
936
936
/// ```
937
937
/// use std::ops::AddAssign;
938
938
///
939
- /// struct Foo;
939
+ /// #[derive(Debug)]
940
+ /// struct Point {
941
+ /// x: i32,
942
+ /// y: i32,
943
+ /// }
940
944
///
941
- /// impl AddAssign for Foo {
942
- /// fn add_assign(&mut self, _rhs: Foo) {
943
- /// println!("Adding!");
945
+ /// impl AddAssign for Point {
946
+ /// fn add_assign(&mut self, other: Point) {
947
+ /// *self = Point {
948
+ /// x: self.x + other.x,
949
+ /// y: self.y + other.y,
950
+ /// };
944
951
/// }
945
952
/// }
946
953
///
947
- /// # #[allow(unused_assignments)]
948
- /// fn main() {
949
- /// let mut foo = Foo;
950
- /// foo += Foo;
954
+ /// impl PartialEq for Point {
955
+ /// fn eq(&self, other: &Self) -> bool {
956
+ /// self.x == other.x && self.y == other.y
957
+ /// }
951
958
/// }
959
+ ///
960
+ /// let mut point = Point { x: 1, y: 0 };
961
+ /// point += Point { x: 2, y: 3 };
962
+ /// assert_eq!(point, Point { x: 3, y: 3 });
952
963
/// ```
953
964
#[ lang = "add_assign" ]
954
965
#[ stable( feature = "op_assign_traits" , since = "1.8.0" ) ]
0 commit comments