Skip to content

Commit fe62b41

Browse files
committed
Clarify Index/IndexMut trait docs
Use examples and placeholder variable names with more meaning, to not make it so abstract.
1 parent a5dbf8a commit fe62b41

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/libcore/ops.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,7 +1873,7 @@ macro_rules! shr_assign_impl_all {
18731873
shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
18741874

18751875
/// The `Index` trait is used to specify the functionality of indexing operations
1876-
/// like `arr[idx]` when used in an immutable context.
1876+
/// like `container[index]` when used in an immutable context.
18771877
///
18781878
/// # Examples
18791879
///
@@ -1924,50 +1924,50 @@ pub trait Index<Idx: ?Sized> {
19241924
#[stable(feature = "rust1", since = "1.0.0")]
19251925
type Output: ?Sized;
19261926

1927-
/// The method for the indexing (`Foo[Bar]`) operation
1927+
/// The method for the indexing (`container[index]`) operation
19281928
#[stable(feature = "rust1", since = "1.0.0")]
19291929
fn index(&self, index: Idx) -> &Self::Output;
19301930
}
19311931

19321932
/// The `IndexMut` trait is used to specify the functionality of indexing
1933-
/// operations like `arr[idx]`, when used in a mutable context.
1933+
/// operations like `container[index]`, when used in a mutable context.
19341934
///
19351935
/// # Examples
19361936
///
1937-
/// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
1938-
/// calling `index_mut`, and therefore, `main` prints `Indexing!`.
1937+
/// A trivial implementation of `IndexMut` for a type `Foo`. When `&mut Foo[2]`
1938+
/// happens, it ends up calling `index_mut`, and therefore, `main` prints
1939+
/// `Mutable indexing with 2!`.
19391940
///
19401941
/// ```
19411942
/// use std::ops::{Index, IndexMut};
19421943
///
19431944
/// #[derive(Copy, Clone)]
19441945
/// struct Foo;
1945-
/// struct Bar;
19461946
///
1947-
/// impl Index<Bar> for Foo {
1947+
/// impl Index<usize> for Foo {
19481948
/// type Output = Foo;
19491949
///
1950-
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
1950+
/// fn index(&self, _index: usize) -> &Foo {
19511951
/// self
19521952
/// }
19531953
/// }
19541954
///
1955-
/// impl IndexMut<Bar> for Foo {
1956-
/// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
1957-
/// println!("Indexing!");
1955+
/// impl IndexMut<usize> for Foo {
1956+
/// fn index_mut(&mut self, index: usize) -> &mut Foo {
1957+
/// println!("Mutable indexing with {}!", index);
19581958
/// self
19591959
/// }
19601960
/// }
19611961
///
19621962
/// fn main() {
1963-
/// &mut Foo[Bar];
1963+
/// &mut Foo[2];
19641964
/// }
19651965
/// ```
19661966
#[lang = "index_mut"]
19671967
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
19681968
#[stable(feature = "rust1", since = "1.0.0")]
19691969
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
1970-
/// The method for the indexing (`Foo[Bar]`) operation
1970+
/// The method for the mutable indexing (`container[index]`) operation
19711971
#[stable(feature = "rust1", since = "1.0.0")]
19721972
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
19731973
}

0 commit comments

Comments
 (0)