8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /// The `Index` trait is used to specify the functionality of indexing operations
12
- /// like `container[index]` when used in an immutable context.
11
+ /// Used for indexing operations (`container[index]`) in immutable contexts.
13
12
///
14
13
/// `container[index]` is actually syntactic sugar for `*container.index(index)`,
15
14
/// but only when used as an immutable value. If a mutable value is requested,
16
15
/// [`IndexMut`] is used instead. This allows nice things such as
17
- /// `let value = v[index]` if `value` implements [`Copy`].
16
+ /// `let value = v[index]` if the type of `value` implements [`Copy`].
18
17
///
19
18
/// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
20
19
/// [`Copy`]: ../../std/marker/trait.Copy.html
64
63
#[ rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`" ]
65
64
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
66
65
pub trait Index < Idx : ?Sized > {
67
- /// The returned type after indexing
66
+ /// The returned type after indexing.
68
67
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
69
68
type Output : ?Sized ;
70
69
71
- /// The method for the indexing (`container[index]`) operation
70
+ /// Performs the indexing (`container[index]`) operation.
72
71
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
73
72
fn index ( & self , index : Idx ) -> & Self :: Output ;
74
73
}
75
74
76
- /// The `IndexMut` trait is used to specify the functionality of indexing
77
- /// operations like `container[index]` when used in a mutable context.
75
+ /// Used for indexing operations (`container[index]`) in mutable contexts.
78
76
///
79
77
/// `container[index]` is actually syntactic sugar for
80
78
/// `*container.index_mut(index)`, but only when used as a mutable value. If
81
79
/// an immutable value is requested, the [`Index`] trait is used instead. This
82
- /// allows nice things such as `v[index] = value` if `value` implements [`Copy`].
80
+ /// allows nice things such as `v[index] = value` if the type of `value`
81
+ /// implements [`Copy`].
83
82
///
84
83
/// [`Index`]: ../../std/ops/trait.Index.html
85
84
/// [`Copy`]: ../../std/marker/trait.Copy.html
@@ -106,7 +105,7 @@ pub trait Index<Idx: ?Sized> {
106
105
///
107
106
/// struct Balance {
108
107
/// pub left: Weight,
109
- /// pub right:Weight,
108
+ /// pub right: Weight,
110
109
/// }
111
110
///
112
111
/// impl Index<Side> for Balance {
@@ -131,28 +130,26 @@ pub trait Index<Idx: ?Sized> {
131
130
/// }
132
131
/// }
133
132
///
134
- /// fn main() {
135
- /// let mut balance = Balance {
136
- /// right: Weight::Kilogram(2.5),
137
- /// left: Weight::Pound(1.5),
138
- /// };
139
- ///
140
- /// // In this case balance[Side::Right] is sugar for
141
- /// // *balance.index(Side::Right), since we are only reading
142
- /// // balance[Side::Right], not writing it.
143
- /// assert_eq!(balance[Side::Right],Weight::Kilogram(2.5));
144
- ///
145
- /// // However in this case balance[Side::Left] is sugar for
146
- /// // *balance.index_mut(Side::Left), since we are writing
147
- /// // balance[Side::Left].
148
- /// balance[Side::Left] = Weight::Kilogram(3.0);
149
- /// }
133
+ /// let mut balance = Balance {
134
+ /// right: Weight::Kilogram(2.5),
135
+ /// left: Weight::Pound(1.5),
136
+ /// };
137
+ ///
138
+ /// // In this case, `balance[Side::Right]` is sugar for
139
+ /// // `*balance.index(Side::Right)`, since we are only *reading*
140
+ /// // `balance[Side::Right]`, not writing it.
141
+ /// assert_eq!(balance[Side::Right], Weight::Kilogram(2.5));
142
+ ///
143
+ /// // However, in this case `balance[Side::Left]` is sugar for
144
+ /// // `*balance.index_mut(Side::Left)`, since we are writing
145
+ /// // `balance[Side::Left]`.
146
+ /// balance[Side::Left] = Weight::Kilogram(3.0);
150
147
/// ```
151
148
#[ lang = "index_mut" ]
152
149
#[ rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`" ]
153
150
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
154
151
pub trait IndexMut < Idx : ?Sized > : Index < Idx > {
155
- /// The method for the mutable indexing (`container[index]`) operation
152
+ /// Performs the mutable indexing (`container[index]`) operation.
156
153
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
157
154
fn index_mut ( & mut self , index : Idx ) -> & mut Self :: Output ;
158
155
}
0 commit comments