Loop - Rust by Example
Loop - Rust by Example
2023 13:04
loop
Rust provides a loop keyword to indicate an infinite loop.
The break statement can be used to exit a loop at anytime, whereas the continue
statement can be used to skip the rest of the iteration and start a new one.
1 fn main() {
2 let mut count = 0u32;
3
4 println!("Let's count until infinity!");
5
6 // Infinite loop
7 loop {
8 count += 1;
9
10 if count == 3 {
11 println!("three");
12
13 // Skip the rest of this iteration
14 continue;
15 }
16
17 println!("{}", count);
18
19 if count == 5 {
20 println!("OK, that's enough");
21
22 // Exit this loop
23 break;
24 }
25 }
26 }
https://doc.rust-lang.org/rust-by-example/flow_control/loop.html Stránka 1 z 1