I can write an immutable variable in rust

73 Views Asked by At
  let mut a = Box::new("123".to_string());
  let b = Box::new( &mut a);

  b.push('4');

  assert_eq!( "1234", b.as_str());

  // lets see the types:
  // let x001: Box<&mut Box<String>> = b;
  // let x001:&mut Box<String> = *b;
  // let x001:Box<String> = **b;
  // let x001:String = ***b;
  // let x001:str = ****b;

  let c = Box::new("456".to_string());

  **b = c;

  b.push('9');
  (*b).push('9');
  (**b).push('9');
  (***b).push('9');
  // (****b).push('9'); // no method named `push` found for type `str` in the current scope

  // c.push( 'a'); // cannot mutate immutable variable `c`

  assert_eq!( "4569999", b.as_str());

  ***b = "abcd".to_string();

  b.push('9');

  assert_eq!( "abcd9", b.as_str());

Variable c is implicitly declared immutable but after putting it into the box it can be mutated because of ownership transfer?

But in the end that would mean we can change every immutable variable.

Can someone explain?

1

There are 1 best solutions below

0
cafce25 On BEST ANSWER

(Im-)mutability is a property of the binding, not the value.

let c = String::from("c");
let mut a = c;
a.push_str("whatever");

also works.

And you're doing essentialy the same:

**b = c;

moves the value of c into the place pointed at by mutable reference stored in b which is a, which is a mutable binding.