Skip to content

Commit 39676c8

Browse files
committed
Fix/update core::ops module documentation w.r.t. operator traits
* Not all traits are part of the prelude anymore * We switched from pass-by-reference to pass-by-value for most traits * Add some explanations around pass-by-value traits in the context of generic code and additional implementations for reference types.
1 parent 5eb254b commit 39676c8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/libcore/ops.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
//! Implementing these traits allows you to get an effect similar to
1414
//! overloading operators.
1515
//!
16-
//! The values for the right hand side of an operator are automatically
17-
//! borrowed, so `a + b` is sugar for `a.add(&b)`.
18-
//!
19-
//! All of these traits are imported by the prelude, so they are available in
16+
//! Some of these traits are imported by the prelude, so they are available in
2017
//! every Rust program.
2118
//!
19+
//! Many of the operators take their operands by value. In non-generic
20+
//! contexts involving built-in types, this is usually not a problem.
21+
//! However, using these operators in generic code, requires some
22+
//! attention if values have to be reused as opposed to letting the operators
23+
//! consume them. One option is to occasionally use `clone()`.
24+
//! Another option is to rely on the types involved providing additional
25+
//! operator implementations for references. For example, for a user-defined
26+
//! type `T` which is supposed to support addition, it is probably a good
27+
//! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>`
28+
//! so that generic code can be written without unnecessary cloning.
29+
//!
2230
//! # Example
2331
//!
2432
//! This example creates a `Point` struct that implements `Add` and `Sub`, and then

0 commit comments

Comments
 (0)