Skip to content

Commit ffa327b

Browse files
author
lukaramu
committed
Revise Index and IndexMut docs.
Part of rust-lang#29365. * Shortened summary sentences, removing "stuttering" * Small copyediting * Changed method summary sentences to be in 3rd person singular * Removed extraneous explicit `fn main()` in example for `IndexMut`
1 parent 4b945fd commit ffa327b

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

src/libcore/ops/index.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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.
1312
///
1413
/// `container[index]` is actually syntactic sugar for `*container.index(index)`,
1514
/// but only when used as an immutable value. If a mutable value is requested,
1615
/// [`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`].
1817
///
1918
/// [`IndexMut`]: ../../std/ops/trait.IndexMut.html
2019
/// [`Copy`]: ../../std/marker/trait.Copy.html
@@ -64,22 +63,22 @@
6463
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
6564
#[stable(feature = "rust1", since = "1.0.0")]
6665
pub trait Index<Idx: ?Sized> {
67-
/// The returned type after indexing
66+
/// The returned type after indexing.
6867
#[stable(feature = "rust1", since = "1.0.0")]
6968
type Output: ?Sized;
7069

71-
/// The method for the indexing (`container[index]`) operation
70+
/// Performs the indexing (`container[index]`) operation.
7271
#[stable(feature = "rust1", since = "1.0.0")]
7372
fn index(&self, index: Idx) -> &Self::Output;
7473
}
7574

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.
7876
///
7977
/// `container[index]` is actually syntactic sugar for
8078
/// `*container.index_mut(index)`, but only when used as a mutable value. If
8179
/// 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`].
8382
///
8483
/// [`Index`]: ../../std/ops/trait.Index.html
8584
/// [`Copy`]: ../../std/marker/trait.Copy.html
@@ -106,7 +105,7 @@ pub trait Index<Idx: ?Sized> {
106105
///
107106
/// struct Balance {
108107
/// pub left: Weight,
109-
/// pub right:Weight,
108+
/// pub right: Weight,
110109
/// }
111110
///
112111
/// impl Index<Side> for Balance {
@@ -131,28 +130,26 @@ pub trait Index<Idx: ?Sized> {
131130
/// }
132131
/// }
133132
///
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);
150147
/// ```
151148
#[lang = "index_mut"]
152149
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
153150
#[stable(feature = "rust1", since = "1.0.0")]
154151
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.
156153
#[stable(feature = "rust1", since = "1.0.0")]
157154
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
158155
}

0 commit comments

Comments
 (0)