Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Iteration in RUST: Working With Loop, While, and For

DZone's Guide to

Iteration in RUST: Working With Loop, While, and For

We look at three types of iterators built-in to the Rust language (for, while, and loop), and which one works best for your needs.

· Web Dev Zone ·
Free Resource

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

I hope you have already been through the following posts in order to learn control flow and iteration in Rust:

Basically, Rust is no different compared to other programming languages in the context of loops and iteration. If you have already worked with any language like C, C++, Java, Scala, etc., then it will be much easier and quicker to go through this blog and get ready for handling the control flow of your logic.

  • It will always have at least one execution.
  • It will execute the block of code forever until we explicitly ask it to stop.
  • If we don't a provide condition within the block to stop, the program will run for infinite and we need to stop the whole program execution.
fn main() {
  loop {
    println!("Printing forever!");
  }
}

The above code will never terminate. To terminate it, either you need to press ctrl+c or change the code to provide an explicit condition to break the flow, as below:

fn main() {
  let mut count = 0;
  loop {
    if count == 5 {
      break;
    }
    println!("Printing 5 times!");
    count = count + 1;
  }
}

Although this is not the preferred method, as it may lead to an infinite execution problem, the second kind of loop is while, which requires a condition:

fn main() {
  let mut count = 0;
  while count < 5 {
    println!("Printing 5 times");
    count = count + 1;
  }
}

I hope you are able to ssee the major differences between loop and while.

The last and the most powerful iterator in Rust isforwhich has multiple use cases. The safety and conciseness of for loops make them the most commonly used loop construct in Rust.

for loops in Rust are much different from for loops in other languages and much similar than the for-each loop. Basically, it needs an iterator to fetch values one by one, let's explain it with the example:

fn main() {
  let a = [10, 20, 30, 40, 50];
  for element in a.iter() {
    println!("the value is: {}", element);
  }
}

Here, we are in a much safer state, as we are not required to declare an index variable, so we increment it with each iteration and explicitly give a condition for iteration. In other programming languages, if we have iterated over an index which is greater than the size of the array then it will create a panic situation, but in Rust, while dealing with for and iterator it will never exceed the index limit and only process the limited indexes.

Summary 

I think you have already realized when to use for, while, or loop. The best and safest approach is to use for, as it assures us of the index limit, while dealing with  while can create panic situations if the condition(s) isn't given properly; and  loop needs even more careful handling as it can lead to infinite execution.

Reference: Control Flow - The Rust Programming Language

This article was first published on the Knoldus blog.

Deploy code to production now. Release to users when ready. Learn how to separate code deployment from user-facing feature releases with LaunchDarkly.

Topics:
web dev ,rust ,iterator ,for loops ,loops

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}