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

Para repetições infinitas que continuam até serem explicitamente paradas com break.

Acesse: Loop sem break

loop + break

#![allow(unused)]
fn main() {
loop {
    println!("Hello, world!");
    break;
}
}

loop + if

#![allow(unused)]
fn main() {
let mut count = 0;

loop {
    println!("Contagem: {}", count);
    count += 1;

    if count == 5 {
        break;
    }
}

println!("Fim do Loop!");
}

loop + break + continue

#![allow(unused)]
fn main() {
let mut count = 0;
let max_valor = 7;

loop {
    count += 1;
    if count % 2 == 0 {
        continue;
    }

    println!("Ímpar = {}", count);
    if count >= max_valor {
        break;
    }

    println!("---")
}
}