loop

Rustにはloopというキーワードが存在します。これは無限ループを作成するのに使用します。

訳注: while Trueと同じですが、ループのたびに条件を確認しないため、若干高速になります。

ループから抜けだす時はbreak, 即座に次のループに移るときはcontinueが使用できます。

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

    println!("Let's count until infinity!");

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

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

            // Skip the rest of this iteration
            // 残りの処理をスキップ
            continue;
        }

        println!("{}", count);

        if count == 5 {
            println!("OK, that's enough");

            // Exit this loop
            // ループを抜ける。
            break;
        }
    }
}
関連キーワード:  関数, count, Result, Rust, Example, By, エラー, ループ, Option, テストケース