trait

IntoIterator — convert a source to an iterator

Accept an iterable source rather than requiring callers to construct a specific iterator. A for loop obtains an iterator using this trait. A reference can itself be the source. Expected stdout (not executed): 25 Common mistake: Assuming into_iter always yields owned elements regardless of receiver.

Formal Syntax & Structure

rust syntaxrust
trait IntoIterator { type Item; type IntoIter: Iterator<Item = Self::Item>; fn into_iter(self) -> Self::IntoIter; }

Working Example

Example Snippetrust
fn total(source: impl IntoIterator<Item = i32>) -> i32 {    source.into_iter().sum()}fn main() { println!("{}", total([12, 8, 5])); }
Rust Reference