Skip to content

Commit dcee93a

Browse files
replace Add example with something more evocative of addition
Currently most of the operator traits use trivial implementation examples that only perform side effects. Honestly, that might not be too bad for the sake of documentation; but anyway, here's a proposal to move a slightly modified version of the module-level point-addition example into the `Add` documentation, since it's more evocative of addition semantics. Part of #29365 wrap identifiers in backticks minor rephrasing fix module-level documentation to be more truthful This branch changes the example for `Add` to no longer be a "minimum implementation that prints something to the screen".
1 parent f65d96f commit dcee93a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/libcore/ops.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
//! }
6363
//! ```
6464
//!
65-
//! See the documentation for each trait for a minimum implementation that
66-
//! prints something to the screen.
65+
//! See the documentation for each trait for an example implementation.
6766
6867
#![stable(feature = "rust1", since = "1.0.0")]
6968

@@ -166,25 +165,38 @@ macro_rules! forward_ref_binop {
166165
///
167166
/// # Examples
168167
///
169-
/// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
170-
/// calling `add`, and therefore, `main` prints `Adding!`.
168+
/// This example creates a `Point` struct that implements the `Add` trait, and
169+
/// then demonstrates adding two `Point`s.
171170
///
172171
/// ```
173172
/// use std::ops::Add;
174173
///
175-
/// struct Foo;
174+
/// #[derive(Debug)]
175+
/// struct Point {
176+
/// x: i32,
177+
/// y: i32,
178+
/// }
176179
///
177-
/// impl Add for Foo {
178-
/// type Output = Foo;
180+
/// impl Add for Point {
181+
/// type Output = Point;
179182
///
180-
/// fn add(self, _rhs: Foo) -> Foo {
181-
/// println!("Adding!");
182-
/// self
183+
/// fn add(self, other: Point) -> Point {
184+
/// Point {
185+
/// x: self.x + other.x,
186+
/// y: self.y + other.y,
187+
/// }
188+
/// }
189+
/// }
190+
///
191+
/// impl PartialEq for Point {
192+
/// fn eq(&self, other: &Self) -> bool {
193+
/// self.x == other.x && self.y == other.y
183194
/// }
184195
/// }
185196
///
186197
/// fn main() {
187-
/// Foo + Foo;
198+
/// assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
199+
/// Point { x: 3, y: 3 });
188200
/// }
189201
/// ```
190202
#[lang = "add"]

0 commit comments

Comments
 (0)