Tutorial by Examples

There are 4 looping constructs in Rust. All examples below produce the same output. Infinite Loops let mut x = 0; loop { if x > 3 { break; } println!("{}", x); x += 1; } While Loops let mut x = 0; while x <= 3 { println!("{}", x); x += 1; ...
As mentioned in Basics, we can use anything which implements IntoIterator with the for loop: let vector = vec!["foo", "bar", "baz"]; // vectors implement IntoIterator for val in vector { println!("{}", val); } Expected output: foo bar baz ...
All looping constructs allow the use of break and continue statements. They affect the immediately surrounding (innermost) loop. Basic Loop Control break terminates the loop: for x in 0..5 { if x > 2 { break; } println!("{}", x); } Output 0 1 2 continue finishes...

Page 1 of 1