about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorMark Simulacrum <mark.simulacrum@gmail.com>2017-05-13 20:55:08 -0600
committerGitHub <noreply@github.com>2017-05-13 20:55:08 -0600
commitc75ccd7b4ee38ed23904d34e94ad659ce7a3c785 (patch)
treecf528bc34b1da84bc97c5cfb6007ee9d72169ca7 /src/libcore
parent93dd1cab5d1ea0ceb178db424d4c907a20584b16 (diff)
parenta2a9d1965b1aba9363f1876de3ed67c0662a294d (diff)
downloadrust-c75ccd7b4ee38ed23904d34e94ad659ce7a3c785.tar.gz
rust-c75ccd7b4ee38ed23904d34e94ad659ce7a3c785.zip
Rollup merge of #41612 - mandeep:add-ops-generics, r=GuillaumeGomez,frewsxcv
Added generic example of std::ops::Add in doc comments

We discussed on IRC how the std::ops examples were potentially missing examples using generics. This PR adds an example to std::ops::Add that shows the use of a generic type T. I'm not sure this is ready for merge as I think the two examples now make the documentation a bit verbose, but I think it's a good starting point. I'd love to hear others thoughts on this. This is in relation to the last item in issue #29365.
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/ops.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs
index 391b606f613..6942f5fb67b 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<T> {
+///     x: T,
+///     y: T,
+/// }
+///
+/// // Notice that the implementation uses the `Output` associated type
+/// impl<T: Add<Output=T>> Add for Point<T> {
+///     type Output = Point<T>;
+///
+///     fn add(self, other: Point<T>) -> Point<T> {
+///         Point {
+///             x: self.x + other.x,
+///             y: self.y + other.y,
+///         }
+///     }
+/// }
+///
+/// impl<T: PartialEq> PartialEq for Point<T> {
+///     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<Duration>`, which permits
 /// operations of the form `SystemTime = SystemTime + Duration`.