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의 용도 중 하나입니다. 만약 작업이 값을 반환한다면, 이를 코드의 나머지 부분으로 전달해야 할 수도 있습니다. break 뒤에 값을 넣으면 loop 표현식에 의해 그 값이 반환됩니다.

fn main() {
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2;
        }
    };

    assert_eq!(result, 20);
}