...

/

Iterators in Rust

Iterators in Rust

Learn the different ways we can iterate over arrays and vectors in Rust.

Iterating over arrays and vectors

The rules of iterating over an array and vector are almost the same:

Using index

This is the most common pattern among various programming languages, Wherein we use a for loop and specify the start and end index of the array:

Press + to interact
Rust 1.57.0
for x in 0..arr.len(){
println!("{}",arr[x]);
}

Using iterator

We can iterate over a collection by using the method into_iter() ...

Access this course and 1400+ top-rated courses and projects.