Skip to content

Implement Index and IndexMut for Vec - #696

Merged
sgued merged 1 commit into
rust-embedded:mainfrom
mikwielgus:index-index-mut-for-vec
Nov 3, 2026
Merged

sgued merged 1 commit into
rust-embedded:mainfrom
mikwielgus:index-index-mut-for-vec

Conversation

@mikwielgus

@mikwielgus mikwielgus commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

While it's already possible to use index syntax (e.g. v[1]) on Vec due to Deref coercion into slice, it's nevertheless useful to have Index and IndexMut traits implemented if you want to have code that is generic over indexable containers via trait bounds, like in this snippet:

fn increment<C>(container: &mut C, index: usize)
where
    C: std::ops::IndexMut<usize>,
    C::Output: std::ops::AddAssign<i32>,
{
    container[index] += 1;
}

Having these traits is consistent with Rust standard library's Vec, which also implements them.

There are also other collections that could implement Index or IndexMut. If this PR is accepted, I will follow up with implementations for these as well.

@mikwielgus
mikwielgus force-pushed the index-index-mut-for-vec branch from 8a64218 to bc8fe8f Compare November 2, 2026 23:29

@sgued sgued left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I didn't realise std::Vec implemented Index directly. The implementation looks good to me, please change the test and update the changelog.

Comment thread src/vec/mod.rs Resolved
@mikwielgus
mikwielgus force-pushed the index-index-mut-for-vec branch from bc8fe8f to 1e054b3 Compare November 3, 2026 17:41
While it's already possible to use index syntax (e.g. `v[1]`) on `Vec`
due to `Deref` coercion into slice, it's nevertheless useful to have
`Index` and `IndexMut` traits implemented if you want to have code that
is generic over indexable containers via trait bounds, like in this
snippet:

```
fn increment<C>(container: &mut C, index: usize)
where
    C: std::ops::IndexMut<usize>,
    C::Output: std::ops::AddAssign<i32>,
{
    container[index] += 1;
}
```

Having these traits is consistent with Rust standard library's `Vec`,
which also implements them.

There are also other collections that could implement `Index`
or `IndexMut`. If this PR is accepted, I will follow up with
implementations for these as well.
@mikwielgus
mikwielgus force-pushed the index-index-mut-for-vec branch from 1e054b3 to f70f19c Compare November 3, 2026 17:43

@sgued sgued left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not the following? Using the trait seems correct but I'm curious, this seems more intuitive.

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

@mikwielgus

Copy link
Copy Markdown
Contributor Author

Why not the following? Using the trait seems correct but I'm curious, this seems more intuitive.

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

I just have a habit of programming defensively when it comes to boilerplatey trait impls, as I ended up with infinitely recursive code a few times due to ambiguities in resolution.

It's probably not needed, but it doesn't make a difference either way.

@sgued

sgued commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Thanks, it also lt also makes it closer to the std::Vec implementation which is good.

@sgued
sgued added this pull request to the merge queue Nov 3, 2026
Merged via the queue into rust-embedded:main with commit 47dfb02 Nov 3, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants