January 2019
Beginner to intermediate
554 pages
13h 31m
English
Repeating things in Rust can be done using three constructs, namely loop, while, and for. In all of them, we have the usual continue and break keywords, which allow you to skip and break out of a loop, respectively. Here's an example of using loop, which is equivalent to C's while(true):
// loops.rs fn main() { let mut x = 1024; loop { if x < 0 { break; } println!("{} more runs to go", x); x -= 1; } }
loop represents an infinite loop. In the preceding code, we simply decrement the value x until it hits the if condition x < 0, where we break out of the loop. An extra feature of using loop in Rust is being able to tag the loop block with a name. This can be used in cases where you have two or more nested loops and want to break ...
Read now
Unlock full access