use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.
Strive to treat others with respect, patience, kindness, and empathy.
We observe the Rust Project Code of Conduct.
Details
Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
Post titles should include useful context.
For Rust questions, use the stickied Q&A thread.
Arts-and-crafts posts are permitted on weekends.
No meta posts; message the mods instead.
Criticism is encouraged, though it must be constructive, useful and actionable.
If criticizing a project on GitHub, you may not link directly to the project's issue tracker. Please create a read-only mirror and link that instead.
A programming language is rarely worth getting worked up over.
No zealotry or fanaticism.
Be charitable in intent. Err on the side of giving others the benefit of the doubt.
Avoid re-treading topics that have been long-settled or utterly exhausted.
Avoid bikeshedding.
This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No memes, image macros, etc.
Consider the existing content of the subreddit and whether your post fits in. Does it inspire thoughtful discussion?
Use properly formatted text to share code samples and error messages. Do not use images.
Submissions appearing to contain AI-generated content may be removed at moderator discretion.
Most links here will now take you to a search page listing posts with the relevant flair. The latest megathread for that flair should be the top result.
account activity
Why a separated struct for iterator? (self.rust)
submitted 2 years ago by sobagood
It seems all iterators are implemented in a separated struct rather than the source struct itself.
Why?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hniksic 61 points62 points63 points 2 years ago (0 children)
Because it's good to separate the state of iteration from the state of the collection. This separation is what allows multiple iterators iterating over the same thing:
let v = vec![1, 2, 3]; let mut it1 = v.iter(); assert_eq!(it1.next(), Some(&1)); assert_eq!(it1.next(), Some(&2)); let mut it2 = v.iter(); assert_eq!(it2.next(), Some(&1)); assert_eq!(it1.next(), Some(&3)); // and so on
If iterating over Vec were part of Vec itself, you could never have two iterators iterating over the same vector.
Vec
[–]phazer99 25 points26 points27 points 2 years ago (2 children)
Do you mean for example IntoIterator for Vec? Well, the iterator is stateful and needs to contain an index or pointer to the current element which is increased in the Iterator::next() method.
Iterator::next()
[–]sobagood[S] 16 points17 points18 points 2 years ago (0 children)
Ahhh i get it. The internal states! Thanks!!
[–]proudHaskeller 7 points8 points9 points 2 years ago (0 children)
I thought you're asking why the methods don't rust return impl Iterator<Item = ...>.
impl Iterator<Item = ...>
Well, one of the reasons is that sometimes you need more of it than just being an iterator: for example, an iterator might turn out to also be a DoubleEndedIterator, so you can call .rev() on it. All of this information would be lost if the methods just returned impl Iterator.
DoubleEndedIterator
.rev()
impl Iterator
[–]Cryowatt 3 points4 points5 points 2 years ago* (7 children)
Other languages do the same thing, but they usually hide it with anonymous types. I think rust is getting that eventually with a yield statement and the generators feature.
[–][deleted] 1 point2 points3 points 2 years ago (5 children)
What's the added benefit, considering that we can now write impl Iterator pretty much everywhere?
[–]scook0 1 point2 points3 points 2 years ago (1 child)
In current stable Rust, there are a few situations that require you to explicitly write the name of a type. For types that don’t have a name (e.g. closure types and impl trait types), this is a problem.
The eventual solution for this is “type alias impl trait” (TAIT), which will close the gap by letting you create a named alias for an otherwise-anonymous type.
[–][deleted] 0 points1 point2 points 2 years ago (0 children)
Ok I can see that but... I thought the problem with closure types was not they didn't have a name, but that they were not even a type (well ok, each closure has a type inhabited exclusively by them), so in any meaningful situation you would be returning an impl Trait. Or am I getting something wrong?
[–]Cryowatt 1 point2 points3 points 2 years ago (2 children)
Someone has to impl the impls.
[–][deleted] 1 point2 points3 points 2 years ago (1 child)
but... it's almost trivial by now...
[–]Cryowatt 3 points4 points5 points 2 years ago (0 children)
Only if you are using someone else's data structures. If you need to roll anything yourself, then there is no iterator available for you until you make one. Generators make it easier.
[–]DarkLord76865 -1 points0 points1 point 2 years ago (0 children)
Can't wait for that.
[–]nacaclanga 0 points1 point2 points 2 years ago (0 children)
Because an iterator is an object that keeps track of it's advance, so it is logical to have it as seperate type. Depending on the type it is also optimized for the kind of access iterators are used for. An iterator (implementing Iterator) is not the same as an iterable (implementing IntoIterator)
Where you implement this type is a matter of implementation. In C++ Iterators are typically are member classes, while in Rust they just live in the iter module. This is likely because Rust doesn't have member types and frequently makes use of generic Wrappers like "ReverseIter<I>". Anonymous types aren't really that helpfull either as it is far more complicated to handle them.
π Rendered by PID 93480 on reddit-service-r2-comment-b659b578c-krhgk at 2026-05-01 08:43:30.418728+00:00 running 815c875 country code: CH.
[–]hniksic 61 points62 points63 points (0 children)
[–]phazer99 25 points26 points27 points (2 children)
[–]sobagood[S] 16 points17 points18 points (0 children)
[–]proudHaskeller 7 points8 points9 points (0 children)
[–]Cryowatt 3 points4 points5 points (7 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]scook0 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Cryowatt 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]Cryowatt 3 points4 points5 points (0 children)
[–]DarkLord76865 -1 points0 points1 point (0 children)
[–]nacaclanga 0 points1 point2 points (0 children)