`impl Iterator` vs `impl IntoIterator`, which is idomatic?

I usually use impl Iterator when I want to return an iterator or consume an iterator. But I found some people are using impl IntoIterator, which makes sense as well.

Which one is the preferred?

Broadly,

  • always use impl IntoIterator as a parameter (it is most flexible, and accepts all iterators), and
  • always return impl Iterator (so the caller doesn't have to immediately write .into_iter()).
16 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.