Mutability

Mutability of data can be changed when ownership is transferred.

  1. fn main() {
  2. let immutable_box = Box::new(5u32);
  3. println!("immutable_box contains {}", immutable_box);
  4. // Mutability error
  5. //*immutable_box = 4;
  6. // *Move* the box, changing the ownership (and mutability)
  7. let mut mutable_box = immutable_box;
  8. println!("mutable_box contains {}", mutable_box);
  9. // Modify the contents of the box
  10. *mutable_box = 4;
  11. println!("mutable_box now contains {}", mutable_box);
  12. }