# Arrays should implement \`Index\` and \`IndexMut\`

**URL:** <https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732>\
**Category:** libs\
**Created:** [July 15, 2020, 8:08am UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732 "2020-07-15T08:08:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pubfnbar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pubfnbar/32/7007_2.png) [@pubfnbar](https://internals.rust-lang.org/u/pubfnbar)\
**Post date:** [July 15, 2020, 8:08am UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732/1 "2020-07-15T08:08:26Z")

</div>

Arrays don't currently implement `Index` and `IndexMut`, but slices do, and arrays coerce to slices in a manner I believe similar to as if they actually implemented `Deref<Target=[I]>`, which they don't. Normally this setup works just fine, but it becomes a problem when you implement `Index<MyIndexType>` for an array, because then you lose all the other `Index` implementations that used to be available to the array via its implicit coercion to a slice. An example of what I'm talking about:

```rust
use std::ops::Index;

pub enum MyIndexType {
    _0, _1, _2, _3, _4, _5, _6, _7,
}

impl<I> Index<MyIndexType> for [I; 8] {
    type Output = I;

    fn index(&self, index: MyIndexType) -> &I {
        unsafe { self.get_unchecked(index as usize) }
    }
}

fn main() {
    let array = [11u8; 8];

    println!("{:?}", array[MyIndexType::_0]); // OK
    
    println!("{:?}", array[0usize]); // error[E0277]
    // ^^^^^^^^^^^^^ `[u8; 8]` cannot be indexed by `usize`
}

```

This problem goes away if we re-implement all the indexing (that is currently provided via the implicit coercion to a slice) directly to all arrays by using const generics. Now that const generics are good enough to be considered for [some other traits](https://github.com/rust-lang/rust/pull/74060), perhaps this would be feasible as well. Or would this be a breaking change?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [July 15, 2020, 5:12pm UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732/2 "2020-07-15T17:12:40Z")

</div>

Historically, the "solution" has been to always impl index on the slice type.

This example shows a legitimate reason to impl index directly on an array type, however: types that are known to index into a fixed sized array can use unchecked indexing.

Of note, of course, is that a forwarding implementation of `Index`[`Mut`] for `[T; N]` would not be for some set of indexing types but instead a blanket impl over any [`SliceIndex<[T]>`](https://doc.rust-lang.org/stable/std/slice/trait.SliceIndex.html) type. The fact that it's a blanket impl makes adding an impl potentially breaking, but I _believe_ that because `SliceIndex` is still unstable to implement that it would be a backwards-compatible change (for stable downstream).

(Alternate design that I really don't know about and potentially requires more than const generics MVP: `impl<I, T; N: usize> SliceIndex<[T; N]> for I where I: SliceIndex<[T]>; impl<I, T, N> Index<I> for [T; N];`. Is this complexity deserved? I honestly don't know yet.)

This would also probably suggest inlining definitions of other indexing methods (anything that takes `impl SliceIndex<Self>`) such as `slice::get` as well.

---

<div class="post-metadata">

**Author:** ![pubfnbar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pubfnbar/32/7007_2.png) [@pubfnbar](https://internals.rust-lang.org/u/pubfnbar)\
**Post date:** [July 16, 2020, 2:07am UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732/3 "2020-07-16T02:07:03Z")

</div>

Just for completeness sake, I believe the forwarding implementation would be literally:

```rust
use std::{
    array::FixedSizeArray,
    ops::{Index, IndexMut},
    slice::SliceIndex,
};

impl<T, I, const N: usize> Index<I> for [T; N]
where I: SliceIndex<[T]>
{
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &I::Output {
        &self.as_slice()[index]
    }
}

impl<T, I, const N: usize> IndexMut<I> for [T; N]
where I: SliceIndex<[T]>
{
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut I::Output {
        &mut self.as_mut_slice()[index]
    }
}

```

---

<div class="post-metadata">

**Author:** ![pubfnbar](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pubfnbar/32/7007_2.png) [@pubfnbar](https://internals.rust-lang.org/u/pubfnbar)\
**Post date:** [July 24, 2020, 2:24pm UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732/4 "2020-07-24T14:24:21Z")

</div>

What should I do in order to try to get these new implementations added to the standard library? Should I create a pull-request to add them to [https://github.com/rust-lang/rust/tree/master/src/libcore/array](https://github.com/rust-lang/rust/tree/master/src/libcore/array)? If so, then it's just that I don't currently know how to do that. I could try to figure it out on my own but I'd prefer it if someone more knowledgeable did the pull-request instead.

EDIT: I created a pull request [here](https://github.com/rust-lang/rust/pull/74989)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 22, 2020, 2:24pm UTC](https://internals.rust-lang.org/t/arrays-should-implement-index-and-indexmut/12732/5 "2020-10-22T14:24:26Z")

</div>

Closed on October 22, 2020, 2:24pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
