|
14 | 14 | //!
|
15 | 15 | //! Some of these traits are imported by the prelude, so they are available in
|
16 | 16 | //! every Rust program. Only operators backed by traits can be overloaded. For
|
17 |
| -//! example, the addition operator (`+`) can be overloaded through the `Add` |
| 17 | +//! example, the addition operator (`+`) can be overloaded through the [`Add`] |
18 | 18 | //! trait, but since the assignment operator (`=`) has no backing trait, there
|
19 | 19 | //! is no way of overloading its semantics. Additionally, this module does not
|
20 | 20 | //! provide any mechanism to create new operators. If traitless overloading or
|
|
30 | 30 | //! contexts involving built-in types, this is usually not a problem.
|
31 | 31 | //! However, using these operators in generic code, requires some
|
32 | 32 | //! attention if values have to be reused as opposed to letting the operators
|
33 |
| -//! consume them. One option is to occasionally use `clone()`. |
| 33 | +//! consume them. One option is to occasionally use [`clone()`]. |
34 | 34 | //! Another option is to rely on the types involved providing additional
|
35 | 35 | //! operator implementations for references. For example, for a user-defined
|
36 | 36 | //! type `T` which is supposed to support addition, it is probably a good
|
37 |
| -//! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>` |
38 |
| -//! so that generic code can be written without unnecessary cloning. |
| 37 | +//! idea to have both `T` and `&T` implement the traits [`Add<T>`][`Add`] and |
| 38 | +//! [`Add<&T>`][`Add`] so that generic code can be written without unnecessary |
| 39 | +//! cloning. |
39 | 40 | //!
|
40 | 41 | //! # Examples
|
41 | 42 | //!
|
42 |
| -//! This example creates a `Point` struct that implements `Add` and `Sub`, and |
43 |
| -//! then demonstrates adding and subtracting two `Point`s. |
| 43 | +//! This example creates a `Point` struct that implements [`Add`] and [`Sub`], |
| 44 | +//! and then demonstrates adding and subtracting two `Point`s. |
44 | 45 | //!
|
45 | 46 | //! ```rust
|
46 | 47 | //! use std::ops::{Add, Sub};
|
|
75 | 76 | //! See the documentation for each trait for an example implementation.
|
76 | 77 | //!
|
77 | 78 | //! The [`Fn`], [`FnMut`], and [`FnOnce`] traits are implemented by types that can be
|
78 |
| -//! invoked like functions. Note that `Fn` takes `&self`, `FnMut` takes `&mut |
79 |
| -//! self` and `FnOnce` takes `self`. These correspond to the three kinds of |
| 79 | +//! invoked like functions. Note that [`Fn`] takes `&self`, [`FnMut`] takes `&mut |
| 80 | +//! self` and [`FnOnce`] takes `self`. These correspond to the three kinds of |
80 | 81 | //! methods that can be invoked on an instance: call-by-reference,
|
81 | 82 | //! call-by-mutable-reference, and call-by-value. The most common use of these
|
82 | 83 | //! traits is to act as bounds to higher-level functions that take functions or
|
83 | 84 | //! closures as arguments.
|
84 | 85 | //!
|
85 |
| -//! [`Fn`]: trait.Fn.html |
86 |
| -//! [`FnMut`]: trait.FnMut.html |
87 |
| -//! [`FnOnce`]: trait.FnOnce.html |
88 |
| -//! |
89 |
| -//! Taking a `Fn` as a parameter: |
| 86 | +//! Taking a [`Fn`] as a parameter: |
90 | 87 | //!
|
91 | 88 | //! ```rust
|
92 | 89 | //! fn call_with_one<F>(func: F) -> usize
|
|
99 | 96 | //! assert_eq!(call_with_one(double), 2);
|
100 | 97 | //! ```
|
101 | 98 | //!
|
102 |
| -//! Taking a `FnMut` as a parameter: |
| 99 | +//! Taking a [`FnMut`] as a parameter: |
103 | 100 | //!
|
104 | 101 | //! ```rust
|
105 | 102 | //! fn do_twice<F>(mut func: F)
|
|
118 | 115 | //! assert_eq!(x, 5);
|
119 | 116 | //! ```
|
120 | 117 | //!
|
121 |
| -//! Taking a `FnOnce` as a parameter: |
| 118 | +//! Taking a [`FnOnce`] as a parameter: |
122 | 119 | //!
|
123 | 120 | //! ```rust
|
124 | 121 | //! fn consume_with_relish<F>(func: F)
|
|
140 | 137 | //!
|
141 | 138 | //! // `consume_and_return_x` can no longer be invoked at this point
|
142 | 139 | //! ```
|
| 140 | +//! |
| 141 | +//! [`Fn`]: trait.Fn.html |
| 142 | +//! [`FnMut`]: trait.FnMut.html |
| 143 | +//! [`FnOnce`]: trait.FnOnce.html |
| 144 | +//! [`Add`]: trait.Add.html |
| 145 | +//! [`Sub`]: trait.Sub.html |
| 146 | +//! [`clone()`]: ../clone/trait.Clone.html#tymethod.clone |
143 | 147 |
|
144 | 148 | #![stable(feature = "rust1", since = "1.0.0")]
|
145 | 149 |
|
|
0 commit comments