Updated June 1, 2023
Introduction to Rust for loop
There may be a requirement in the program when we want to execute our code several times; then, we have various flow control for this. In this tutorial, we will discuss loop in Rust. for loop in rust is used to execute the number of lines or code logic to be iterated the number of times. We can define the number of how many times we want this logic to be executed in our code. Inside the for loop, we can use the break and continue keyword to break it or skip the current execution. Using this, we can execute a statement or group of statements any number of times. In the coming section of the tutorial, we will discuss the for loop in detail for a better understanding of its usage in the program.
Syntax
As we discussed, for loop in Rust is used to iterate the number of lines of code. Let’s take a look at its syntax for better usage of for loop while programming see below;
for your_tem_variable in start_point..upperend_point_bound {
// your logic will go here
}
As you can see in the above lines of syntax, we are using the ‘for’ keyword to define ours for loop in Rust. After this, we assign one temporary variable and the number of times we want it executed. Now take a practice syntax for beginners to understand it better and implement it in the program for better usage see below;
Example:
for a in 0..10 {
// our logic goes here ..
}
In the coming section, we will discuss the internal working of it and its implementation in the actual program.
Flowchart
In every programming language, it works in the same way. We will discuss the flow chart for loop in Rust. Let’s see the steps for that in detail see below;
1. Entry point for this is the ‘start’ point by which it gets started.
2. After this, it evaluates the expression we specify in for statement.
3. After consideration of the expression, it comes out to be true or false.
4. Once the evolution of the expression comes out to be true, it goes inside the loop and executes the rest of the logic.
5. If the evaluation of the expression comes out to be false, then it returns false, and it will not go inside the loop, and none of the logic will be executed further.
Below see the flowchart attach for loop in rust, which will give you a better understanding of the flow. see below;
<image>
How for loop works in Rust?
As we have already discussed, for loop is a control flow statement in the programming language. It works in the same way as any other programming language. Rust provides many control flow statements, but we will discuss it here for a loop. Inside the loop, we can also use a break and continue keyword to control its flow. If you want to iterate the array or list elements, then for loop is a good option because for loop are designed to provide this functionality in the programming language. Let’s take a sample example of how to use for loop in rust language see below;
Example:
fn main(){
for temp in 1..5{
println!("{}",temp);
}
}
As you can see in the above lines of code, we are trying to print the element from 1 to 4. But as you can see, we have mentioned the number as 5; it will be inclusive. To make this work, we have used the ‘for’ keyword here; after this, we are holding the value into the temp variable named ‘temp’ only. Inside the for loop, we are trying to print the temp variable. But be very careful while printing the variable in rust; follow the above syntax. Once the value because 5, the loop will be terminated, and inside logic will not be executed.
Points to remember while using for loop in rust which is as follows see below;
- We have to use for loop keyword before the declaration of the statement.
- This is mainly used to iterate the iterable objects to some definite number of times.
- Also, inside the loop, we can write our own logic, which we want to execute a number of times or repeatedly.
- We can also use the break and continue stamen inside it to control the loop’s flow in the Rust programming language. It works in the same way as any other programming language.
Example of Rust for loop
1. In the above example, we are trying to iterate the number from 1 to 10 using for loop in rust. This is a sample example for beginners to understand and implement in the program.
Example:
fn main(){
println!("demo to show for loop in rust language.!!");
println!("for loop start here !!!!");
for temp in 1..10{
print!("value inside loop is :::");
println!("{}",temp);
}
println!("for loop end here !!!!");
}
Output:
2. In this example, we are trying to use the break and continue statement to showcase the flow control of for loop in rust; it is based on the condition if it matched, it will start work.
Example:
fn main(){
println!("demo to show for loop in rust language.!!");
println!("for loop start here !!!!");
for temp in 1..20{
if temp==5 {
println!("trying to see continue keyword based on condition!!!!");
println!("value inside the loop is {}",temp);
continue;
}
print!("value inside loop is :::");
println!("{}",temp);
if temp==15 {
println!("trying to see break keyword based on condition!!!!");
println!("value inside the loop is {}",temp);
break;
}
}
println!("for loop end here !!!!");
}
Output:
Conclusion
By the use of for loops, we can iterate the array, list, or collection in any programming language. They sed when we want to see all the elements of the array. Loops come under the control flow statement in the rust programming language we have a while loop and for in rust programming language.
Recommended Articles
We hope that this EDUCBA information on “Rust for loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.