Skip to content

Implement Index and IndexMut traits - #98

Open
mikwielgus wants to merge 1 commit into
mozilla:mainfrom
mikwielgus:main
Open

mikwielgus wants to merge 1 commit into
mozilla:mainfrom
mikwielgus:main

Conversation

@mikwielgus

Copy link
Copy Markdown

It's useful to have Index and IndexMut traits implemented if you want to have code that is generic over indexable containers, such as this one:

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 also consistent with Vec, which also implements them.

Immutable indexing is already tested, but I have added a test for mutable indexing.

It's useful to have `Index` and `IndexMut` traits implemented if you
want to have code that is generic over indexable containers, such as
this one:

```
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 also consistent with `Vec`, which also
implements them.

Immutable indexing is already tested, but I have added a test for
mutable indexing.
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.

1 participant