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

while

while 키워드는 조건이 참(true)인 동안 루프를 실행하는 데 사용될 수 있습니다.

악명 높은 FizzBuzzwhile 루프를 사용하여 작성해 봅시다.

fn main() {
    // 카운터 변수
    let mut n = 1;

    // `n`이 101보다 작은 동안 루프 실행
    while n < 101 {
        if n % 15 == 0 {
            println!("fizzbuzz");
        } else if n % 3 == 0 {
            println!("fizz");
        } else if n % 5 == 0 {
            println!("buzz");
        } else {
            println!("{}", n);
        }

        // 카운터 증가
        n += 1;
    }
}