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

가변성

변수 바인딩은 기본적으로 불변(immutable)이지만, mut 수식어를 사용하여 이를 재정의할 수 있습니다.

fn main() {
    let _immutable_binding = 1;
    let mut mutable_binding = 1;

    println!("변경 전: {}", mutable_binding);

    // 좋습니다
    mutable_binding += 1;

    println!("변경 후: {}", mutable_binding);

    // 에러! 불변 변수에는 새로운 값을 할당할 수 없습니다
    _immutable_binding += 1;
}

컴파일러는 가변성 에러에 대해 상세한 진단을 제공할 것입니다.