Skip to content

Clarify Index/IndexMut trait docs #36529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,7 @@ macro_rules! shr_assign_impl_all {
shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }

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

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

/// The `IndexMut` trait is used to specify the functionality of indexing
/// operations like `arr[idx]`, when used in a mutable context.
/// operations like `container[index]`, when used in a mutable context.
///
/// # Examples
///
/// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
/// calling `index_mut`, and therefore, `main` prints `Indexing!`.
/// A trivial implementation of `IndexMut` for a type `Foo`. When `&mut Foo[2]`
/// happens, it ends up calling `index_mut`, and therefore, `main` prints
/// `Mutable indexing with 2!`.
///
/// ```
/// use std::ops::{Index, IndexMut};
///
/// #[derive(Copy, Clone)]
/// struct Foo;
/// struct Bar;
///
/// impl Index<Bar> for Foo {
/// impl Index<usize> for Foo {
/// type Output = Foo;
///
/// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
/// fn index(&self, _index: usize) -> &Foo {
/// self
/// }
/// }
///
/// impl IndexMut<Bar> for Foo {
/// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
/// println!("Indexing!");
/// impl IndexMut<usize> for Foo {
/// fn index_mut(&mut self, index: usize) -> &mut Foo {
/// println!("Mutable indexing with {}!", index);
/// self
/// }
/// }
///
/// fn main() {
/// &mut Foo[Bar];
/// &mut Foo[2];
/// }
/// ```
#[lang = "index_mut"]
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
#[stable(feature = "rust1", since = "1.0.0")]
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
/// The method for the indexing (`Foo[Bar]`) operation
/// The method for the mutable indexing (`container[index]`) operation
#[stable(feature = "rust1", since = "1.0.0")]
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
}
Expand Down