trait

FromIterator — build a destination

The destination type decides how to construct itself from incoming items. collect uses this trait; a set may eliminate duplicates rather than retain every element. Expected stdout (not executed): {2, 9} Common mistake: Assuming all destinations preserve order and duplicates.

Formal Syntax & Structure

rust syntaxrust
trait FromIterator<A>: Sized { fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self; }

Working Example

Example Snippetrust
fn main() {    use std::collections::BTreeSet;    let unique = BTreeSet::from_iter([9, 2, 9]);    println!("{unique:?}");}
Rust Reference