# Idea: Unsafe Iterator optimization traits

**URL:** <https://forum.modular.com/t/idea-unsafe-iterator-optimization-traits/3506>\
**Category:** Standard Library\
**Created:** [September 18, 2026, 6:17am UTC](https://forum.modular.com/t/idea-unsafe-iterator-optimization-traits/3506 "2026-09-18T06:17:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![leb-kuchen](https://avatars.discourse-cdn.com/v4/letter/l/a183cd/32.png) [@leb-kuchen](https://forum.modular.com/u/leb-kuchen)\
**Post date:** [September 18, 2026, 6:17am UTC](https://forum.modular.com/t/idea-unsafe-iterator-optimization-traits/3506/1 "2026-09-18T06:17:53Z")

</div>

For reference, rust has [TrustedLen](https://doc.rust-lang.org/stable/std/iter/trait.TrustedLen.html).  
Mojo would arguably have an easier time than rust implementing that, since mojo has `conform_to` and `reflect`. This is one reason why rust iterators are described as zero cost; namely because of these internal optimizations(even though it falls apart at opt-level s or z and custom non-std iterators don’t benefit from those optimizations.).  
So I think, mojo should also take this path, since the goal is maximum performance for higher level abstractions. If rust can do this, mojo should as well.

---

<div class="post-metadata">

**Author:** ![nate](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/nate/32/740_2.png) [@nate](https://forum.modular.com/u/nate)\
**Post date:** [September 18, 2026, 4:40pm UTC](https://forum.modular.com/t/idea-unsafe-iterator-optimization-traits/3506/2 "2026-09-18T16:40:19Z")

</div>

Hi @leb-kuchen - yep this is definitely something we have had the idea of for a long time.

However, before we do any sort of these “optimized” unsafe iterator traits, we need first officially settle how to really model the `Iterator` trait and how to make the ` __next__ ` function return either ` ref T` or a `T`.  
Once we figure this out, then we can do all the fancy optimizations and unsafe traits implementable for the `Iterator` trait family.
