From a2a9d1965b1aba9363f1876de3ed67c0662a294d Mon Sep 17 00:00:00 2001 From: mandeep Date: Fri, 28 Apr 2017 13:16:49 -0500 Subject: [PATCH] Added generic example of std::ops::Add in doc comments Added blank lines around example Added comment to Add example referencing the Output type Removed whitespace from lines 272 and 273 Removed Debug derivation from Add examples Added Debug derivation --- src/libcore/ops.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 175b3a5a69a..03cfdf53783 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -235,6 +235,42 @@ pub trait Drop { /// } /// ``` /// +/// Here is an example of the same `Point` struct implementing the `Add` trait +/// using generics. +/// +/// ``` +/// use std::ops::Add; +/// +/// #[derive(Debug)] +/// struct Point { +/// x: T, +/// y: T, +/// } +/// +/// // Notice that the implementation uses the `Output` associated type +/// impl> Add for Point { +/// type Output = Point; +/// +/// fn add(self, other: Point) -> Point { +/// Point { +/// x: self.x + other.x, +/// y: self.y + other.y, +/// } +/// } +/// } +/// +/// impl PartialEq for Point { +/// fn eq(&self, other: &Self) -> bool { +/// self.x == other.x && self.y == other.y +/// } +/// } +/// +/// fn main() { +/// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 }, +/// Point { x: 3, y: 3 }); +/// } +/// ``` +/// /// Note that `RHS = Self` by default, but this is not mandatory. For example, /// [std::time::SystemTime] implements `Add`, which permits /// operations of the form `SystemTime = SystemTime + Duration`.