Search⌘ K

Arrays and Slices

Explore how to iterate over arrays and slices in Rust by learning two distinct approaches: borrowing slices with & to access iterators and using the .iter() method. This lesson helps you understand why direct iteration over arrays is restricted and how to effectively work around it.

We'll cover the following...

For some complicated reasons that I don’t personally understand, you can’t directly iterate over an array. In other words, this code doesn’t compile:

Rust 1.40.0
fn main() {
let fibs = [1, 1, 2, 3, 5, 8, 13];
for x in fibs {
println!("{}", x);
}
}

Fortunately, we have two ...