Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

loop

Rust は無限ループを示すために loop キーワードを提供しています。

break 文はいつでもループを抜けるために使用でき、一方 continue 文はイテレーションの残りをスキップして新しい イテレーションを開始するために使用できます。

fn main() {
    let mut count = 0u32;

    println!("無限まで数えましょう!");

    // 無限ループ
    loop {
        count += 1;

        if count == 3 {
            println!("3");

            // このイテレーションの残りをスキップ
            continue;
        }

        println!("{}", count);

        if count == 5 {
            println!("OK、もう十分です");

            // このループを抜ける
            break;
        }
    }
}