title: Mutability in fields for structs in Rust
body: <p>I'm still new with Rust but I have doubts about how mutability works for fields in structs. Specifically how we can modify fields that originally were immutable. For example:</p>
  
  <pre><code>struct Point {
      x: isize,
      y: isize,
  }
  
  impl Point {
      fn new(x: isize, y: isize) -&gt; Self {
          Self { x, y }
      }
      fn set_x(&amp;mut self, new_x: isize) {
          self.x = new_x;
      }
  }
  
  struct Line {
      p: Point,
      q: Point,
  }
  
  impl Line {
      fn new(p: Point, q: Point) -&gt; Self {
          Self { p, q }
      }
      fn set_x_in_p(&amp;mut self, new_x: isize) {
          self.p.set_x(new_x);
      }
  }
  
  fn main() {
      // Both x and y are immutable
      let p = Point::new(0, 0);
      let q = Point::new(1, 1);
  
      // Line IS mutable
      let mut line = Line::new(p, q);
  
     // Modifying point p (originally immutable) with a new x 
      line.set_x_in_p(999);
  }
  </code></pre>
  
  <p>Instead with references we cannot </p>
  
  <pre><code>   let x = 3;
     let y = &amp;mut x; // does not work because x originally is immutable
  </code></pre>
  
  <p>So, how does it work? Thanks.</p>