title: Who has ownership of a struct's members in a function that takes &self?
body: <p>I am trying to write a small wrapper around <code>VecDeque</code>. </p>
  
  <p>Specifically I have the code (<a href="https://play.rust-lang.org/?gist=c9fc43f0903b753082ea1c66444d9bcc&amp;version=stable&amp;backtrace=0" rel="nofollow">playground</a>): </p>
  
  <pre><code>use std::collections::VecDeque;
  
  trait VecCircleTraits&lt;T: Eq&gt; {
      fn new() -&gt; VecCircle&lt;T&gt;;
      fn find_and_remove(&amp;self, _: T) -&gt; Option&lt;T&gt;;
  }
  
  #[derive(Debug)]
  struct VecCircle&lt;T: Eq&gt;(VecDeque&lt;T&gt;);
  
  impl&lt;T: Eq&gt; VecCircleTraits&lt;T&gt; for VecCircle&lt;T&gt; {
      fn new() -&gt; VecCircle&lt;T&gt; {
          return VecCircle(VecDeque::&lt;T&gt;::new());
      }
  
      fn find_and_remove(&amp;self, key: T) -&gt; Option&lt;T&gt; {
          let search_index: Option&lt;usize&gt; = self.0.into_iter().position(|x| x == key); //error 1
          if let Some(index) = search_index {
              return self.0.remove(index); // error 2
          } else {
              return None;
          }
      }
  }
  </code></pre>
  
  <p>Which gives me the following errors: </p>
  
  <pre class="lang-none prettyprint-override"><code>    error: cannot borrow immutable anonymous field `self.0` as mutable
    --&gt; &lt;anon&gt;:20:20
     |&gt;
  20 |&gt;             return self.0.remove(index); // error 2
     |&gt;                    ^^^^^^
  
  error: cannot move out of borrowed content [--explain E0507]
    --&gt; &lt;anon&gt;:18:44
     |&gt;
  18 |&gt;         let search_index: Option&lt;usize&gt; =  self.0.into_iter().position(|x| x == key); //error 1
     |&gt;                                            ^^^^ cannot move out of borrowed content
  </code></pre>
  
  <p>However, I am little confused as who has ownership over <code>self.0</code>? If I am understanding the docs correctly, wouldn't the memory region be bounded to <code>self.0</code> and therefore giving it the ownership? Sorry for the shallow logic there but I am still trying to understand the ownership system.</p>