Hello World!
The iter()
function generates an iterator trait to
iterate a collection of values by reference.
This includes arrays, vectors, slices, etc.
The function will return an iterator of type T
,
where T is the reference type of the elements of the collection.
Iterators have a wide array, pun intended,
of functions to help performing actions on all or specific collection items.
There's the next()
, position()
, Map
, Filter
, but most importantly
it can function inside loop structures to define the loop.
The Iterator
trait comes with it the position()
function that,
with the help of a predicate expression passed into it,
find the index of an item in a collection.
Using the function signature,
Iterator.position(predicate: P) -> Option<usize>
,
you simply pass a predicate expression into it and get the index or None in return.
names = ["Bob", "Steve", "Karen", "Lindsey"];
index = names.iter().position(|&n: String| n == "Karen").unrwap();
println!("{} has index of {}", names[index], index)
The above code snippet sets up a vector of name strings.
An index
is set from calling position
with predicate
n == "Karen"
from lambda function of arg |&n: String|
.
Then the index
is found,
since it's found and unwrap
ed it can access the name Karen
.