This is the most simple repetition statement. We use only keyword "loop" and { ... } to create a repetitive block. This will be repeated forever and the program will get stack in the loop. You can stop using CTRL+C. This is actually a logical error. Good programmers must avoid infinite loops at all cost.
Loop Diagram
fn main() {
loop {
println!("press ctrl+c to stop me!");
}
}
Note: This example is not available to be run, it is rude to block the session.
To avoid an infinite loop you must create a break using a conditional stop.In the next example we create a control variable "i" that is declared outside of the loop and start with 1. Then you can increment this variable with +1 every iteration. You can use the relation operator "i > 3". This loop will execute 3 times then will stop and program will terminate.
Loop Break
fn main() {
let mut i = 1;
loop {
println!("Variable i is now {}!",i);
if i > 3 {
break;
} else {
i += 1;
} //end if
} // end loop
} // end main
You can use continue
keyword to start over. This will create a kind of shortcut that will skip all other statements and restart the loop. You can continue a loop from inside a nested block.
Loop Continue
fn main() {
let mut i = 1;
loop {
if i < 3 {
i += 1;
println!("Variable i is now {}!",i);
continue;
} break;
} // end loop
} // end main
This is a repetitive block of code that is executed as long as one condition is true. When the condition become false the program continue with next statement after the loop block end. Now the problem is to create a condition expression that will become false. If the condition never become false we again can have an infinite loop.
While Loop
fn main() {
let mut number = 3;
while number != 0 {
println!("{}!", number);
number = number - 1;
} // end while
println!("LIFTOFF!!!");
} // end main
This kind of loop is created using keyword "for" and it has two usages: One is to iterate a specific number of times over a range of numbers. Second is to iterate over a collection of items: array, vector, string or map/dictionary. The range in Rust is created using a range expression with .. operator. (1..10) will create one number and will assign it to "i" to create the control variable.
For Loop
with continue & break
Syntax
// display numbers 0 to 9;
for i in range {
//first block
if condition {
continue;
}
//second block
if condition {
break;
}
//third block
}//end for
Example 1: Range Loop
Next program demonstrate range 0..10, we observe last number is 9 and not 10. The range do not include the outer limit. This is how ranges work in Rust. This loop do not have any alterations (break/continue) are not used.
// display numbers 0 to 9;
fn main() {
// the lust number i will not be generated that is the Range definition
for i in 0..10 {
println!("{}!", i);
}//end for
println!("done!");
} //end main
Example 2: Nested Loops
Next program demonstrate nested loops. We generate prime numbers. The inner loop has an alteration. We break the loop if the number is not a prime number.
// generate prime numbers < 30
fn main() {
for i in 1..30 {
for j in 2..i {
if (i % j) == 0 {
break;
};
if j == (i-1) {
print!("{},", i);
};
};
}//end for
println!("done!");
} //end main
Homework: This example is available on repl.it for testing. open on repl.it and run it. Then modify the program to generate prime numbers < 1000.
This is the most useful loop that is actually the iterative loop. It will visit each item from a collection until the last element is visited. The peculiar thing specific to Rust is that we have to specify method "iter()" to get an iterator that is actually a function that can be used to fetch the next element.
fn main() {
let a = [10, 20, 30, 40, 50];
// next we iterate over all members of array "a"
for element in a.iter() {
println!("the value is: {}", element);
} // end for
} // end main
Read next: Functions