# Why do Iterator methods use \`usize\`?

**URL:** https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891
**Category:** libs
**Created:** [April 15, 2015, 7:44pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891 "2015-04-15T19:44:46Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![aochagavia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aochagavia/32/5783_2.png) [@aochagavia](https://internals.rust-lang.org/u/aochagavia)
#### Post date: [April 15, 2015, 7:44pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/1 "2015-04-15T19:44:46Z")

</div>

I would expect them to use `u32` or `u64`, since they aren’t necessarily tied to a datastructure of `usize` elements (like `Vec`). Should I file an issue?

Some examples:

```rust
fn size_hint(&self) -> (usize, Option<usize>) { ... }
fn nth(&mut self, n: usize) -> Option<Self::Item> { ... }
fn skip(self, n: usize) -> Skip<Self> { ... }
fn take(self, n: usize) -> Take<Self> { ... }
fn position<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool { ... }
fn rposition<P>(&mut self, predicate: P) -> Option<usize> where Self: ExactSizeIterator + DoubleEndedIterator, P: FnMut(Self::Item) -> bool { ... }

```

---

<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: [April 15, 2015, 8:09pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/2 "2015-04-15T20:09:58Z")

</div>

You’d have to file it in the RFC repo, since it’s a major API change – and we’re past the beta, so it really has to be an emergency to go through 😄

I’ll explain why it’s not a very big deal.

The core API is this:

`fn next(&mut self) -> Option<Self::Item>`

It has no assumption about length and can deliver elements infinitely. So infinite or longer than current `usize::MAX` iterators are supported.

`size_hint` has one primary purpose: to size allocations well when creating collections from iterators. In general, these are limited by `usize` too, so the types match well. Of course it does not match with sizing other kinds of resources, for example files. Fortunately it’s just a hint. The protocol specifies you should use `usize::MAX` as the bound if it is known to be larger than usize (including infinite).

The other methods, `nth`, `skip`, `take`, `position` are all un/fortunately **O(index)** complexity methods, which means that in practice you really want to avoid using big numbers here. Position is frequently useful with vecs/slices, which use usize indices.

I can see that it looks limiting… In the worst case though, they are just adaptors and library code can create their own, maybe there is a use case for `.take64(n: u64)` or even bigger!

---

<div class="post-metadata">

### Author: ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)
#### Post date: [April 15, 2015, 9:04pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/3 "2015-04-15T21:04:58Z")

</div>

It’s basically completely arbitrary, but it does serve a few use cases, where it’s referencing a container that contains a non-zero-sized structure that lives in the RAM.

---

<div class="post-metadata">

### Author: ![TyOverby](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tyoverby/32/363_2.png) [@TyOverby](https://internals.rust-lang.org/u/TyOverby)
#### Post date: [April 16, 2015, 12:23am UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/4 "2015-04-16T00:23:44Z")

</div>

Iterators aren’t backed by a specific data structure, but they certainly can by implemented by specific data structures. Those data structures might require usize.

---

<div class="post-metadata">

### Author: ![tbu](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tbu](https://internals.rust-lang.org/u/tbu)
#### Post date: [April 16, 2015, 11:14pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/5 "2015-04-16T23:14:58Z")

</div>

But you can as well say: Iterators aren’t backed by specific data structures, but they can be implemented by data structures that require {`u32`, `u64`, etc.}.

---

<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: [April 17, 2015, 2:09pm UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/6 "2015-04-17T14:09:47Z")

</div>

It occurred to me that the nth, skip, take adaptors are overridable now again. I still think that doesn’t make them very viable for random access since the structs they must return are fixed. Maybe a future mature random access api can be formed and the question of type for the offsets will come up again.

---

<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:24am UTC](https://internals.rust-lang.org/t/why-do-iterator-methods-use-usize/1891/7 "2019-03-25T08:24:17Z")

</div>

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