In Rust, we can loop through a vector using a for
loop. Here's the syntax:
main.rs89 chars5 lines
In the code above, we first create a vector
containing the integers 1 to 5. We then use a for
loop to iterate over each element in the vector, printing each element to the console.
Alternatively, we can use a for
loop with a range to access the elements of the vector by index:
main.rs94 chars5 lines
Here, we use a for
loop with a range that goes from 0
to vector.len() - 1
, accessing each element of the vector using its index.
gistlibby LogSnag