title: Difference in mutability between reference and box
body: <p>I'm trying to understand Rust pointer types and their relation to mutability. Specifically, the ways of declaring a variable which holds the pointer and is itself mutable -- i.e. can be pointed to some other memory, and declaring that the <em>data itself</em> is mutable -- i.e. can be changed <em>through</em> the value of the pointer variable.</p>
  
  <p>This is how I understand plain references work:</p>
  
  <pre><code>let mut a = &amp;5; // a is a mutable pointer to immutable data
  let b = &amp;mut 5; // b is an immutable pointer to mutable data
  </code></pre>
  
  <p>So <code>a</code> can be changed to point to something else, while <code>b</code> can't. However, the data to which <code>b</code> points to can be changed through <code>b</code>, while it can't through <code>a</code>. <strong>Do I understand this correctly?</strong></p>
  
  <p>For the second part of the question -- why does <code>Box::new</code> seem to behave differently? This is my current understanding:</p>
  
  <pre><code>let mut a = Box::new(5); // a is a mutable pointer to mutable data
  let c = Box::new(7); // c is an immutable pointer to immutable data
  </code></pre>
  
  <p><code>new</code> should return a pointer to some heap-allocated data, but the data it points to seems to inherit mutability from the variable which holds the pointer, unlike in the example with references where these two states of mutability are independent! <strong>Is that how <code>Box::new</code> is supposed to work?</strong> If so, how can I create a pointer value to mutable data on the heap that is stored in an immutable variable?</p>