Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Solidity While Loop
Next article icon

Rust - While Loop

Last Updated : 27 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 loop keyword is used to create a loop. While loops are similar to while loops in other languages. In a while loop, we have a condition and a block of statements. The condition is checked at the start of each iteration of the loop. If the condition is true, the block of statements is executed and the loop continues. If the condition is false, the loop terminates. We will cover the while loop in detail in this article.

While loop

Note:

The while loop comes under the flow control category.

Syntax:

while condition {

// code block

// counter increment

}

Let's dig into the while loop.

  1. The while keyword is used to create a loop. It is followed by a condition.
  2. The condition should be valid and it should be followed by a block of code. If the condition is true, the block of code is executed. If the condition is false, the loop terminates.
  3. The code block or the statements is a group of instructions or operations that we perform after entering the loop. Here we have the task that we want to repeat.
  4. For the counter condition or the update expression after executing the loop body, we need to increment the loop variable to keep the loop running. It is a necessary condition, without this, the while loop might not end and become an infinite loop.

Let's take some examples to better understand the while loop in Rust

Example 1:

Rust
// while loop in Rust fn main(){     // creating a counter variable      let mut n = 1;      // loop while n is less than 6     while n < 6 {         // block of statements          println!("Hey Geeks!!");          // increment counter         n += 1;      } // exiting while loop } 

In the above piece of code, we have created a simple program where we are printing the string "Hey Geeks!!" multiple times using the while loop. 

Output: 

figure 1: While loop

Example 2: To multiply 2 with every number from 1 to 10 using the while loop.

Rust
// Rust program to multiply numbers fn main(){      // number to multiply with      let num = 2;          // A mutable variable i     let mut i = 1; //       // while loop run till i is less than 11     while i < 11 {                  // printing the multiplication         println!("{}", num*i);          // increment counter         i += 1;     } } 

Output: 

Figure 2: While loop

Next Article
Solidity While Loop

A

amnindersingh1414
Improve
Article Tags :
  • Rust
  • rust-control-flow

Similar Reads

  • R - while loop
    While loop in R programming language is used when the exact number of iterations of a loop is not known beforehand. It executes the same code again and again until a stop condition is met. While loop checks for the condition to be true or false n+1 times rather than n times. This is because the whil
    5 min read
  • Swift - While Loop
    Just like other programming languages, the working of the while loop in Swift is also the same. It is used to execute a target statement repeatedly as long as a given condition is true. And when the condition becomes false, the loop will immediately break and the line after the loop will execute. Wh
    2 min read
  • While loop Syntax
    While loop is a fundamental control flow structure (or loop statement) in programming, enabling the execution of a block of code repeatedly as long as a specified condition remains true. Unlike the for loop, which is tailored for iterating a fixed number of times, the while loop excels in scenarios
    5 min read
  • Solidity While Loop
    In Solidity, a while loop is a type of loop statement that allows you to execute a block of code repeatedly until a certain condition is met.  Syntax: while (condition) {    statement or block of code to be executed if the condition is True } The condition is an expression that is evaluated before e
    1 min read
  • PL/SQL While Loop
    Oracle PL/SQL provides various loop structures that help developers execute a block of code multiple times based on certain conditions. The main loop structures include LOOP ... END LOOP, WHILE ... END LOOP, and FOR ... END LOOP. In this article, we will explore the WHILE loop in detail, including i
    5 min read
  • Do While loop Syntax
    Do while loop is a control flow statement found in many programming languages. It is similar to the while loop, but with one key difference: the condition is evaluated after the execution of the loop’s body, ensuring that the loop’s body is executed at least once. Table of Content Do While loop Synt
    5 min read
  • Swift - Repeat While loop
    Sometimes there's a situation that comes when we have to execute a block of many numbers of times so to do this task we use the Repeat While loop. Or we can say that in Swift, we use the Repeat While loop to execute a block of code or a set of statements repeatedly. Repeat...while loop is almost sam
    3 min read
  • SAP ABAP | While Loop
    A fundamental control structure in SAP ABAP is the while loop. The while loop in SAP ABAP can be used to iterate a part of code while the given condition remains true. This construct the while loop facilitates iterative running a block of code continues to execute until the specified condition withi
    3 min read
  • Solidity Do While Loop
    In solidity do while loop is similar to the other programming languages firstly it won't check the condition it executes the program at least once which is written in the do block and later it checks the condition in the while block. Syntax: do{ // Block of code to be written for execution }while(co
    2 min read
  • PostgreSQL - While Loops
    When working with PostgreSQL, knowing how to efficiently use loops can be essential for running iterative operations. PostgreSQL’s WHILE loop allows developers to repeatedly execute a block of code as long as a specified condition remains true. PostgreSQL provides the loop statement which simply def
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences