Arrays are placed on the stack, since their size is static and always needs to be predefined.
Iterator is returned with
Array is typed like this:
[u32, 5] - 5 elements of type
Each array with different type and size is like a separate type in Rust.
Another collection type. It’s dynamic, so it’s placed on the heap (it’s a
Vec<T> is like a
List<T> in C#. They are more expensive than
arrays. Elements are placed next to each other in memory.
Operations on vectors:
The second way of reading will not panic if we try to access element out of
index. It will return
Iterators give access to consecutive elements in collections. Iterators
Iterator trait, which exposes
next() method and
It’s easy to define our own iterators.
Iterators provide zero-cost abstractions, they are very performant.
There are three methods on collections that return iterators:
iter()- iterates over immutable references to items in the collection
iter_mut()- iterates over mutable references to items in the collection
into_iter()- returns owned values from the collection
Some methods defined on the
Iterator trait take ownership of the iterator
and return something useful. An example is the
collect() function consumes iterator into an actual collection
Some other methods can transform an iterator into another kind of iterator. These iterators are lazy, so to actually do anything the resulting iterator needs to be consumed.
map()- takes a closure to be executed agains each item and returns closure’s result. It’s similar to JS’s
filter()- returns an iterator that filters source collection based on a closure returning bool.
skip()- skips some items.