A loop is a programming structure that repeats a sequence of instructions until a specific condition is satisfied. Similar to other programming languages, Rust also has two types of loops:
- Indefinite Loop: While loop and Loop
- Definite Loops: For loop
Let's explore them in detail.
While Loop
A simple way of explaining a while loop is that the loop runs until the given condition is met. For the below loop we have taken user input, and we decrement until it reaches 1.
While the loop starts by checking the condition, Here, we create the variable with local scope to the loop, or we can use an already existing variable which marks the start of the loop to use in use the loop used to check the condition. If it is true, then the loop is executed. For this reason, it is also called the Entry control loop. Once the condition is true, the statements in the loop body are executed and the loop contains a variable that is updated for each iteration. When the condition becomes false or explicitly asked to break, the loop terminates which marks the end of its life cycle.
Syntax:
while condition {
statements;
}
Example 1: A Rust program to print numbers from 10 to 1.
Rust
fn main() {
let mut n = 10;
while n!=0 {
println!("{}", n);
n-=1;
}
}
Output:
10
9
8
7
6
5
4
3
2
1
Example 2: Program to print multiplication table of 2 using while loop.
Rust
fn main() {
let mut n = 1;
while n<=10 {
println!("{}", n*2);
n+=1;
}
}
Output:
2
4
6
8
10
12
14
16
18
20
For loop
The for loop similar to the while loop has a definite start and endpoint as well as the increment for each iteration. In the below loop we have taken the user input and check whether the input is below or above 5.
For loop is somewhat different from while loop. Here also, we create the variable with local scope to the loop, or we can use an already existing variable which marks the start of the loop to use in use the loop. The declared variable is also used to declare the termination condition of the loop. For all the values in between initial value and termination value the return a boolean value. It is also an Entry Control Loop as the condition is checked prior to the execution of the loop statements. Then the condition is checked to true the loop will be executed and if the value is false or explicitly defined to be exited the loop terminates. The iteration value is updated for each iteration.
Syntax:
for iterator in range{
statements;
}
Example 1: Program to print numbers from 1 to 10 using For loop.
Rust
fn main() {
for n in 1..11 {
println!("{}", n);
}
}
Output:
1
2
3
4
5
6
7
8
9
10
Example 2: Program to print multiplication table of 2 using For loop.
Rust
fn main() {
for i in 1..11 {
println!("{}", i*2);
}
}
Output:
2
4
6
8
10
12
14
16
18
20
Loop statement
Rust also supports another looping statement called the loop statement. Loop statement executes until there is a break statement. Break statement (or) break keyword is used to exit the loop.
Syntax:
loop {
statements;
// for exit
break;
}
Example 1: Program to print multiplication table of 2 using the Loop statement.
Rust
fn main() {
let mut n = 1;
loop {
println!("{}", n*2);
n+=1;
if n>10{
break;
}
}
}
Output:
2
4
6
8
10
12
14
16
18
20
Similar Reads
Rust - While Loop
Loops in Rust come into use when we need to repeatedly execute a block of statements. Loops are also useful when we need to iterate over a collection of items. In Rust, we have various kinds of loops, including loops, while loops, and for loops. The while loop is the most common loop in Rust. The lo
3 min read
File I/O in Rust
File I/O in Rust enables us to read from and write to files on our computer. To write or read or write in a file, we need to import modules (like libraries in C++) that hold the necessary functions. This article focuses on discussing File I/O in Rust. Methods for File I/O Given below are some method
7 min read
Rust - Literals
A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix. Example:  Integer literal 6 h
1 min read
Rust - If let Operator
The basic difference between conventional if-else and if let is that if let uses one pattern that needs to be matched to an expression also known as scrutinee, if the pattern matches the expression then the corresponding code is executed, or in most cases that value is assigned according to it. Synt
2 min read
JS++ | Loops
It often happens in computer programming that you want a piece of code to execute more than once. Loops are programming statements that allow you to handle such cases. JS++ contains loops of various kinds, and we will look at several of them in this tutorial. We will begin, however, by examining two
9 min read
Standard I/O in Rust
In this article, we will explore the standard input and standard output in the Rust programming language. Generally, the standard input is provided through the keyboard and the standard output displays values in the console. There are two core traits in Rust, around which the Rust's standard Input
4 min read
Rust - Enum
An enum in Rust is a custom data type that represents data that can be anyone among several possible variants. Each variant in the enum can optionally have data associated with it. An enumerated type is defined using the enum keyword before the name of the enumeration. It also consists of methods. S
3 min read
Conditionals in Rust
Conditional Statements are the decision-making statements based on given conditions. Conditional statements are common in programming languages and rust has them and unlike many of them, the boolean condition doesn't need to be surrounded by parentheses but is better to use, and each condition is fo
3 min read
Rust - Multiple Bounds
In Rust, we have a concept of multiple bounds. Multiple bounds are defined under the Traits of Rust programming. Multiple bounds in Rust can be applied with a '+' operator and the types are separated using comma (,) For needing more than one bounds in Rust programs, we need to import use::std::fmt::
2 min read
Rust - HashMaps
The concept of HashMap is present in almost all programming languages like Java, C++, Python, it has key-value pairs and through key, we can get values of the map. Keys are unique no duplicates allowed in the key but the value can be duplicated. 1. Insertion In HashMap : To insert a value in a HashM
4 min read