Search⌘ K

Definite Loop - For Loop

Explore how to implement definite loops in Rust using the for loop. Understand the syntax, iterating over ranges, and using the enumerate function to track iteration counts. This lesson helps you gain control over loop execution, preparing you for more advanced looping concepts in Rust.

What Is a for Loop?

A for loop is a definite loop, meaning, the number of iterations is defined.

Syntax

The for loop is followed by a variable that iterates over a list of values.

The general syntax is :

Example

The following example uses a for loop that prints 5 numbers.

Rust 1.40.0
fn main() {
//define a for loop
for i in 0..5 {
println!("{}", i);
}
}

Explanation

for loop definition

...