title: how to fix cannot borrow as mutable, as it is behind a `&` reference
body: <p>I am new to rust and trying to learn and experiment, here is a link to the playground with the issue and explanation below: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19920813410a42500045cf2c6cc94f12" rel="nofollow noreferrer">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=19920813410a42500045cf2c6cc94f12</a></p>
  <pre class="lang-rust prettyprint-override"><code>use std::fmt;
  
  #[derive(Clone, Copy)]
  struct Animal&lt;'a&gt; {
      name: &amp;'a str
  }
  
  impl &lt;'a&gt;Animal&lt;'a&gt; {
      fn new(name: &amp;'a str) -&gt;  Self { Self { name } }    
  }
  struct Barn&lt;'a&gt; {
      name: &amp;'a str,
      animals: Vec&lt;Animal&lt;'a&gt;&gt;
  }
  
  impl &lt;'a&gt; Barn&lt;'a&gt; {
      fn new(name: &amp;'a str, animals: Vec&lt;Animal&lt;'a&gt;&gt;) -&gt;  Self { Self { name, animals } }    
  }
  
  impl &lt;'a&gt;fmt::Debug for Barn&lt;'a&gt; {
      fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
          write!(f, &quot;{}&quot;, self.name)
      }
  }
  
  struct Farmer&lt;'a&gt; {
      name: &amp;'a str,
      barns: Vec&lt;Barn&lt;'a&gt;&gt;
  }
  
  impl &lt;'a&gt; Farmer&lt;'a&gt; {
      fn new(name: &amp;'a str, barns: Vec&lt;Barn&lt;'a&gt;&gt;) -&gt; Self { Self { name, barns } }
      
      fn add_new_barn(&amp;mut self, barn: Barn&lt;'a&gt;) {
          self.barns.push(barn)
      }
      
      fn place_animal_in_barn(&amp;mut self, animal: Animal, placement: &amp;str) {
          for barn in &amp;self.barns {
              // how do I do make this work?
              if barn.name == placement {
                  barn.animals.push(animal);
              }
          }
      }
  }
  
  impl &lt;'a&gt;fmt::Debug for Farmer&lt;'a&gt; {
      fn fmt(&amp;self, f: &amp;mut fmt::Formatter) -&gt; fmt::Result {
          write!(f, &quot;{:#?}&quot;, self.barns)
      }
  }
  
  fn main() {
      let mut farmer = Farmer::new(&quot;Farm Master Joe&quot;,
          vec![
              Barn::new(&quot;Tegridy Farms&quot;, vec![
                  Animal::new(&quot;cow&quot;), 
                  Animal::new(&quot;horse&quot;), 
                  Animal::new(&quot;pigeon&quot;)
              ]), 
              Barn::new(&quot;Barney&quot;, vec![
                  Animal::new(&quot;Purple Dinosaur&quot;), 
                  Animal::new(&quot;Green Dinosaur&quot;)
              ]), 
              Barn::new(&quot;White Rabbits R' Us&quot;, vec![
                  Animal::new(&quot;white rabbit&quot;),
              ])
          ]
      );
      
      println!(&quot;{:#?}&quot;, farmer.name);
      farmer.add_new_barn(Barn::new(&quot;The Good Place&quot;, vec![
          Animal::new(&quot;Horse&quot;),
          Animal::new(&quot;Wombat&quot;)
      ]));
      
      farmer.place_animal_in_barn(Animal::new(&quot;Turkey&quot;), &quot;The Good Place&quot;);
      println!(&quot;{:#?}&quot;, farmer);
  }
  </code></pre>
  <p>How do I fix this method, so I can push to the vector?</p>
  <pre class="lang-rust prettyprint-override"><code>    fn place_animal_in_barn(&amp;mut self, animal: Animal, placement: &amp;str) {
          for barn in &amp;self.barns {
              // how do I do make this work?
              if barn.name == placement {
                  barn.animals.push(animal);
              }
          }
      }
  
  </code></pre>
  <p>I am getting the following error and I don't quite understand it or how to fix it:</p>
  <pre class="lang-rust prettyprint-override"><code>  |
  39 |         for barn in &amp;self.barns {
     |                     ----------- this iterator yields `&amp;` references
  ...
  42 |                 barn.animals.push(animal);
     |                 ^^^^^^^^^^^^ `barn` is a `&amp;` reference, so the data it refers to cannot be borrowed as mutable
  </code></pre>