Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I can write an immutable variable in rust

  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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

(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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading