whilenumbers[index]for ... in ...HashSetHashSetDoes not compile! HashSet is not an ordered collection
cannot index into a value of type `&HashSet<i32>`
nextNumbersIterator!&i32 valuesIterator is statefulNumbersIterator) needs to keep track of its position within the collection&'a [i32] and do *self = rest, but that doesn’t work for HashSet<i32>!begin and end pointers)T everywhere:Iterator<T> multiple times on the same type for different T
Iterator trait in the Rust standard library
How can we implement a product function?
fold algorithmB and combine (“fold”) elements using a function ffold(0, +) == sumfold(1, *) == productIterator traitOption<T>::mapStrings and want to parse them all to integers:next() returns Option<String> and we want to apply the parsing function only if it is SomeOption<T>::mapStrings and want to parse them all to integers:.map() for us on every next() call?map algorithmOption::mapmap algorithmmapnext and map calls…while let Some(next) = iter.next() patternfor item in iter!next() anymore!FromIterator!FromIterator is complicatedtrait FromIterator<A> {
// Convert `iter` into an iterator first
// Then convert that iterator into `Self`
// `Self` will typically be a collection, but doesn't have to be
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self;
}
pub trait IntoIterator {
// The type of the elements being iterated over.
type Item;
// Which kind of iterator are we turning this into?
type IntoIter: Iterator<Item = Self::Item>;
// Convert `self` into an iterator. Consumes `self`!
// This is a bit like `From` and `Into`!
fn into_iter(self) -> Self::IntoIter;
}IntoIterator and also provide .iter()/.iter_mut().keys(), .values() etc.for-loop patterns can be converted into algorithms with speakable names:map returns a new iterator but doesn’t call .next() -> It is lazycollect evaluates the iterator by calling .next() until it yields None -> It is eagerfilter does no work and allocates no memoryfor ... in ... syntaxall, any, filter, find etc.
collect, count, fold, for_each etc.
enumerate, filter, flatten, map etc.
chain, unzip, zip
cmp, eq, is_partitioned, partition etc.
Where is sort?
sort functions are available on the collections themselves (i.e. Vec::sort)for-loopsfor-loops: iter().filter().map().collect()#[inline] attributefold…