about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorMatthew Piziak <matthew.piziak@gmail.com>2016-08-18 16:54:33 -0400
committerMatthew Piziak <matthew.piziak@gmail.com>2016-08-18 16:58:38 -0400
commit6c66eaa035e5fc47ebbff44b81d2cb3cf1d7d568 (patch)
treeba1dfc9c2a8cd1fffabd583cda9df363eb24b0f5 /src/libcore
parent11f880588791930cb130071c2cb972fc3c3354ed (diff)
downloadrust-6c66eaa035e5fc47ebbff44b81d2cb3cf1d7d568.tar.gz
rust-6c66eaa035e5fc47ebbff44b81d2cb3cf1d7d568.zip
replace `AddAssign` example with something more evocative of addition
This is analogous to PR #35709 for the `Add` trait.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/ops.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs
index 9347ac2a8c8..1a203a898f7 100644
--- a/src/libcore/ops.rs
+++ b/src/libcore/ops.rs
@@ -894,25 +894,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
 ///
 /// # Examples
 ///
-/// A trivial implementation of `AddAssign`. When `Foo += Foo` happens, it ends up
-/// calling `add_assign`, and therefore, `main` prints `Adding!`.
+/// This example creates a `Point` struct that implements the `AddAssign`
+/// trait, and then demonstrates add-assigning to a mutable `Point`.
 ///
 /// ```
 /// use std::ops::AddAssign;
 ///
-/// struct Foo;
+/// #[derive(Debug)]
+/// struct Point {
+///     x: i32,
+///     y: i32,
+/// }
 ///
-/// impl AddAssign for Foo {
-///     fn add_assign(&mut self, _rhs: Foo) {
-///         println!("Adding!");
+/// impl AddAssign for Point {
+///     fn add_assign(&mut self, other: Point) {
+///         *self = Point {
+///             x: self.x + other.x,
+///             y: self.y + other.y,
+///         };
 ///     }
 /// }
 ///
-/// # #[allow(unused_assignments)]
-/// fn main() {
-///     let mut foo = Foo;
-///     foo += Foo;
+/// impl PartialEq for Point {
+///     fn eq(&self, other: &Self) -> bool {
+///         self.x == other.x && self.y == other.y
+///     }
 /// }
+///
+/// let mut point = Point { x: 1, y: 0 };
+/// point += Point { x: 2, y: 3 };
+/// assert_eq!(point, Point { x: 3, y: 3 });
 /// ```
 #[lang = "add_assign"]
 #[stable(feature = "op_assign_traits", since = "1.8.0")]