0

What I'd like to be able to do is rely on existing containers to handle however they contain multiple items, and use their existing iterator to iterate over the container. I am aware of the existing questions:

So, I've gathered that it's not possible in Rust to keep a reference to the container and its iterator in a custom struct, but I'm looking for a solution that would allow my custom struct to implement Iterator to iterate over the container in my custom struct.

I've created the simplest example of what I'm trying to do:

How can I modify UserStore to support iteration? Or is this just not possible in Rust and need to essentially reimplement all the container functionality in UserStore itself?

E-rich
  • 9,243
  • 11
  • 48
  • 79
  • 3
    You'll find looking at the standard collections that the data structure itself does not implement `Iterator`, they use a separate type. If you follow that pattern for a custom wrapper, the problem in the three linked Q&As does not appear. – kmdreko Jan 29 '23 at 02:22
  • I read twice the question but it still unclear to me, what do you want ? Don't tell what you tried, tell me what you want do to. Why do you want a wrapper iterator thing. – Stargateur Jan 29 '23 at 03:12
  • @Stargateur I want `UserStore` to use `HashMap` but still support iterating through the `HashMap`'s items. e.g. `for user in user_store { println!("{user}"); }` – E-rich Jan 29 '23 at 03:20
  • https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=7928a9b07ac0e509507dad21da08d09a, thus hashmap iterator is unordered so your assert are wrong. – Stargateur Jan 29 '23 at 03:28
  • Ugh, I knew there had to be a simple solution. Thanks @Stargateur – E-rich Jan 29 '23 at 03:39

0 Answers0