Summary
-
for
loops iterate over the values in anIterator
-
Iterator
is a trait that represents a stream of values -
Range expressions like
1..11
or1..=10
are one kind ofIterator
-
Many types provide an
IntoIterator
implementation, includingVec
and slices -
With the rule of 3, you can get:
-
Moved iterators of values (
.into_iter()
) -
Reference iterators (borrow the value, or
.iter()
) -
Mutable reference iterators (mutably borrow the value, or
.iter_mut()
)
-
-
To iterate on a string slice, you need to choose either
.chars()
or.bytes()
-
You can write generic iterator functions using
Iterator
as a trait bound -
Iterators have a lot more power for the future, allowing a lot of functional programming
Get hands-on with 1400+ tech skills courses.