Skip to content

Commit 16ce5dc

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#35806 - matthew-piziak:addassign-example, r=steveklabnik
replace `AddAssign` example with something more evocative of addition This is analogous to PR rust-lang#35709 for the `Add` trait.
2 parents 20b4b99 + 6c66eaa commit 16ce5dc

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
@@ -930,25 +930,36 @@ shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
930930
///
931931
/// # Examples
932932
///
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`.
935935
///
936936
/// ```
937937
/// use std::ops::AddAssign;
938938
///
939-
/// struct Foo;
939+
/// #[derive(Debug)]
940+
/// struct Point {
941+
/// x: i32,
942+
/// y: i32,
943+
/// }
940944
///
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+
/// };
944951
/// }
945952
/// }
946953
///
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+
/// }
951958
/// }
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 });
952963
/// ```
953964
#[lang = "add_assign"]
954965
#[stable(feature = "op_assign_traits", since = "1.8.0")]

0 commit comments

Comments
 (0)