cannot borrow as immutable because it is also borrowed as mutable
<p>I'm using the structs <code>Foo</code> and <code>Bar</code> from a library and I'm getting a compilation error in the client code. I simplified the code to this:</p>

<pre><code>use std::marker::PhantomData;

struct Foo {
    some_str: &amp;'static str,
}

struct Bar&lt;'a&gt; {
    some_str: &amp;'static str,
    marker: PhantomData&lt;&amp;'a Foo&gt;,
}

impl Foo {
    fn read_data(&amp;self) {
        // add code here
    }
    fn create_bar&lt;'a&gt;(&amp;'a mut self) -&gt; Bar&lt;'a&gt; {
        Bar {
            some_str: "test2",
            marker: PhantomData,
        }
    }
}

fn process(_arr: &amp;mut [Bar]) {}

fn main() {
    let mut foo = Foo { some_str: "test" };
    let mut array: [Bar; 1] = [foo.create_bar()];
    process(&amp;mut array);
    foo.read_data();
}
</code></pre>

<p>(<a href="https://play.rust-lang.org/?gist=b47a832014a7b1df0bca&amp;version=stable" rel="nofollow noreferrer">playground</a>)</p>

<p>Output:</p>

<pre class="lang-none prettyprint-override"><code>error[E0502]: cannot borrow `foo` as immutable because it is also borrowed as mutable
  --&gt; src/main.rs:30:5
   |
28 |     let mut array: [Bar; 1] = [foo.create_bar()];
   |                                --- mutable borrow occurs here
29 |     process(&amp;mut array);
30 |     foo.read_data();
   |     ^^^ immutable borrow occurs here
31 | }
   | - mutable borrow ends here
</code></pre>

<p>The error in the console output is very clear, but I cannot fix the problem. </p>