O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loops

Simple loops are programmed with either the while loop (if a condition for the looping is wanted) or with loop (if no condition is wanted). The break keyword gets you out of the loop. Here's an example of using the loop keyword:

// loop.rs fn main() {   let mut x = 1000;   loop {         if x < 0 {                  break;             }             println!("{} more runs to go", x);             x -= 1;     } } 

An example of while loop is as follows:

// while.rs fn main() {   let mut x = 1000;   while x > 0 {             println!("{} more runs to go", x);             x -= 1;       } } 

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required