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!("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;
        }
    }
}