Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- VueJS
- Go
Practical Paper
Industrial Training
Rust Loop
If we want to execute the block of statements more than once, then loops concept comes under the role. A loop executes the code present inside the loop body till the end and starts again immediately from the starting.
Rust consists of three kinds of loops:
-
loops
for loop
while loop
loop
The loop is not a conditional loop. It is a keyword that tells the Rust to execute the block of code over again and again until and unless you explicitly stop the loop manually.
Syntax of loop
loop{ //block statements }
In the above syntax, block statements are executed infinite times.
Flow diagram of loop:
Let's see a simple example of infinite loop
fn main() loop { println!("Hello javaTpoint"); }
Output:
Hello javaTpoint Hello javaTpoint Hello javaTpoint Hello javaTpoint . . . infinite times
In this example, "Hello javaTpoint" is printed over and over again until and unless we stop the loop manually. Generally, "ctrl+c" command is used to terminate from the loop.
Termination from loops
The 'Break' keyword is used to terminate from the loop. If 'break' keyword is not used then the loop will be executed infinite times.
Let's see a simple example
fn main() let mut i=1; loop { println!("Hello javaTpoint"); if i==7 { break; } i+=1; }}
Output:
Hello javaTpoint Hello javaTpoint Hello javaTpoint Hello javaTpoint Hello javaTpoint Hello javaTpoint Hello javaTpoint
In the above example, i is a counter variable, and it is a mutable variable which conveys that the counter variable can be changed for the future use.