Custom iterator for references


#1

I have a custom iterator on a struct. Now if the struct is a references, the iterator gives me this error: no method named `iter` found for type `&result::Table in the current scope`

It worked by implementing IntoIterator for the reference to the struct. However, then I need to call into_iter() to use it.

How can I implement a custom iterator (with .iter() method) for references to structs?

Thx


#2

Can you provide the relevant code so we have more context to better answer the question?


#3

Why don’t you like the IntoIterator for a reference to the struct approach?

iter() is not part of any builtin trait, so you’d just impl that function on your struct type (taking &self as an arg) and return a type that implements Iterator. But as @leodasvacas mentioned, you should provide more info.


#5

Thanks vitalyd. This was the information I needed. Having a .iter() method, my code looks more nice and is easier to understand.

Cheers,
Ronny