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

可変性

所有権が移動されると、データの可変性を変更できます。

fn main() {
    let immutable_box = Box::new(5u32);

    println!("immutable_box contains {}", immutable_box);

    // 可変性エラー
    //*immutable_box = 4;

    // ボックスを*ムーブ*し、所有権(および可変性)を変更する
    let mut mutable_box = immutable_box;

    println!("mutable_box contains {}", mutable_box);

    // ボックスの内容を変更する
    *mutable_box = 4;

    println!("mutable_box now contains {}", mutable_box);
}