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

可変性

変数束縛はデフォルトでは不変ですが、これは mut 修飾子を使用して上書きできます。

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

    println!("Before mutation: {}", mutable_binding);

    // OK
    mutable_binding += 1;

    println!("After mutation: {}", mutable_binding);

    // エラー!不変変数に新しい値を代入することはできません
    _immutable_binding += 1;
}

コンパイラは、可変性エラーに関する詳細な診断を出力します。