# \`positions()\` method for slices, or iterators in general

**URL:** <https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921>\
**Category:** libs\
**Created:** [September 15, 2017, 6:26am UTC](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921 "2017-09-15T06:26:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmccombs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tmccombs/32/1845_2.png) [@tmccombs](https://internals.rust-lang.org/u/tmccombs)\
**Post date:** [September 15, 2017, 6:26am UTC](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921/1 "2017-09-15T06:26:32Z")

</div>

I recently came across a situation where I wanted to get the indices of each occurrence of a value in a slice. Basically, something like `str.match_indices`, but for a `&[u8]`. I was surprised to learn that there wasn’t anything like that in the standard library. The best thing I could find would be to do something like

```rust
slice.iter().enumerate().filter(|(_,x)| x == needle).map(|(i,_)| i)

```

I expected there to be something like

```rust
slice.iter().positions(|x| x == needle)

```

Do you think a positions method should be added to Iterator in the standard library? Or to slice? If so, I’d be happy to make a PR and/or RFC.

_Poll ([view on site](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921/1))_

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [September 15, 2017, 7:49am UTC](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921/2 "2017-09-15T07:49:12Z")

</div>

There’s an issue open on `itertools` to add exactly this method to it: [https://github.com/bluss/rust-itertools/issues/218](https://github.com/bluss/rust-itertools/issues/218)

I think it would be best to see how much it gets used in practice before deciding to add that method, which is just syntactic sugar for a combination of methods and which can’t be improved over the default impl for any implementation

---

<div class="post-metadata">

**Author:** ![bluss](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bluss/32/2264_2.png) [@bluss](https://internals.rust-lang.org/u/bluss)\
**Post date:** [September 15, 2017, 4:48pm UTC](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921/3 "2017-09-15T16:48:15Z")

</div>

Looks like came up with the same idea at the same time… we have the interest needed to put it into itertools! (I voted std. lib. anyway.)

---

<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:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/positions-method-for-slices-or-iterators-in-general/5921/4 "2019-03-25T08:29:00Z")

</div>

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