Loops
https://doc.rust-lang.org/book/loops.html
Loop labels( 条件付きgoto )がよい。
サンプルコードはcontinue
だけだったが、break
でも意図通り動く。
fn main() { 'outer: for x in 0..10 { 'inner: for y in 0..10 { if x % 2 == 0 { continue 'outer; } // continues the loop over x if y % 2 == 0 { continue 'inner; } // continues the loop over y println!("x: {}, y: {}", x, y); } } 'outer2: for x in 0..10 { 'inner2: for y in 0..10 { if x % 2 == 0 { continue 'outer2; } // continues the loop over x if x % 5 == 0 { break 'outer2; } // break the loop over x if y % 2 == 0 { continue 'inner2; } // continues the loop over y println!("x: {}, y: {}", x, y); } } }
$ cargo run Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/4_6_loops` x: 1, y: 1 x: 1, y: 3 x: 1, y: 5 x: 1, y: 7 x: 1, y: 9 x: 3, y: 1 x: 3, y: 3 x: 3, y: 5 x: 3, y: 7 x: 3, y: 9 x: 5, y: 1 x: 5, y: 3 x: 5, y: 5 x: 5, y: 7 x: 5, y: 9 x: 7, y: 1 x: 7, y: 3 x: 7, y: 5 x: 7, y: 7 x: 7, y: 9 x: 9, y: 1 x: 9, y: 3 x: 9, y: 5 x: 9, y: 7 x: 9, y: 9 x: 1, y: 1 x: 1, y: 3 x: 1, y: 5 x: 1, y: 7 x: 1, y: 9 x: 3, y: 1 x: 3, y: 3 x: 3, y: 5 x: 3, y: 7 x: 3, y: 9