<p dir="auto">The following code does not compile:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::rc::Rc;
use std::cell::RefCell;

pub struct Callbacks {
    callbacks: Vec&lt;Rc&lt;RefCell&lt;FnMut(i32)&gt;&gt;&gt;,
}

impl Callbacks {
    pub fn call(&amp;mut self, val: i32) {
        for callback in self.callbacks.iter() {
            let mut closure = callback.borrow_mut();
            closure(val);
        }
    }
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>rc<span class="pl-kos">::</span><span class="pl-v">Rc</span><span class="pl-kos">;</span>
<span class="pl-k">use</span> std<span class="pl-kos">::</span>cell<span class="pl-kos">::</span><span class="pl-v">RefCell</span><span class="pl-kos">;</span>

<span class="pl-k">pub</span> <span class="pl-k">struct</span> <span class="pl-smi">Callbacks</span> <span class="pl-kos">{</span>
    <span class="pl-c1">callbacks</span><span class="pl-kos">:</span> <span class="pl-smi">Vec</span><span class="pl-kos">&lt;</span><span class="pl-smi">Rc</span><span class="pl-kos">&lt;</span><span class="pl-smi">RefCell</span><span class="pl-kos">&lt;</span><span class="pl-smi">FnMut</span><span class="pl-kos">(</span><span class="pl-smi">i32</span><span class="pl-kos">)</span><span class="pl-kos">&gt;</span><span class="pl-kos">&gt;</span><span class="pl-kos">&gt;</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span>

<span class="pl-k">impl</span> <span class="pl-smi">Callbacks</span> <span class="pl-kos">{</span>
    <span class="pl-k">pub</span> <span class="pl-k">fn</span> <span class="pl-en">call</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">self</span><span class="pl-kos">,</span> <span class="pl-s1">val</span><span class="pl-kos">:</span> <span class="pl-smi">i32</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">for</span> callback <span class="pl-k">in</span> <span class="pl-smi">self</span><span class="pl-kos">.</span><span class="pl-c1">callbacks</span><span class="pl-kos">.</span><span class="pl-en">iter</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
            <span class="pl-k">let</span> <span class="pl-k">mut</span> closure = callback<span class="pl-kos">.</span><span class="pl-en">borrow_mut</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
            <span class="pl-en">closure</span><span class="pl-kos">(</span>val<span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">(<a href="http://is.gd/oQk3Fm" rel="nofollow">Playpen</a>)</p>
<p dir="auto">It says: "cannot borrow immutable borrowed content as mutable".</p>
<p dir="auto">Instead, I have to explicitly dereference and borrow the closure in the loop, with <code class="notranslate">(&amp;mut *closure)(val)</code>. Typically, Rust performs this automatically, so I expected that to also happen in this case. At the very least, the error message should be improved, as currently, it doesn't help with the actual issue at all.</p>
<p dir="auto">Testcase:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::ops::*;
struct S&lt;'a, T:FnMut() + 'static + ?Sized&gt;(&amp;'a mut T);
impl&lt;'a, T:?Sized + FnMut() + 'static&gt; DerefMut for S&lt;'a, T&gt; {
    fn deref_mut(&amp;mut self) -&gt; &amp;mut Self::Target { &amp;mut self.0 }
}
impl&lt;'a, T:?Sized + FnMut() + 'static&gt; Deref for S&lt;'a, T&gt; {
    type Target = FnMut() + 'a;
    fn deref(&amp;self) -&gt; &amp;Self::Target { &amp;self.0 }
}
fn main() {
    let mut f = ||{};
    let mut s = S(&amp;mut f);
    s();
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>ops<span class="pl-kos">::</span><span class="pl-c1">*</span><span class="pl-kos">;</span>
<span class="pl-k">struct</span> <span class="pl-smi">S</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">,</span> <span class="pl-smi">T</span><span class="pl-kos">:</span><span class="pl-smi">FnMut</span><span class="pl-kos">(</span><span class="pl-kos">)</span> + <span class="pl-c1">'</span><span class="pl-ent">static</span> + ?<span class="pl-smi">Sized</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-c1">'</span><span class="pl-ent">a</span> <span class="pl-k">mut</span> <span class="pl-smi">T</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">impl</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">,</span> <span class="pl-smi">T</span><span class="pl-kos">:</span>?<span class="pl-smi">Sized</span> + <span class="pl-smi">FnMut</span><span class="pl-kos">(</span><span class="pl-kos">)</span> + <span class="pl-c1">'</span><span class="pl-ent">static</span><span class="pl-kos">&gt;</span> <span class="pl-smi">DerefMut</span> <span class="pl-k">for</span> <span class="pl-smi">S</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">,</span> <span class="pl-smi">T</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">fn</span> <span class="pl-en">deref_mut</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">self</span><span class="pl-kos">)</span> -&gt; <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">Self</span><span class="pl-kos">::</span><span class="pl-smi">Target</span> <span class="pl-kos">{</span> <span class="pl-c1">&amp;</span><span class="pl-k">mut</span> <span class="pl-smi">self</span><span class="pl-kos">.</span><span class="pl-c1">0</span> <span class="pl-kos">}</span>
<span class="pl-kos">}</span>
<span class="pl-k">impl</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">,</span> <span class="pl-smi">T</span><span class="pl-kos">:</span>?<span class="pl-smi">Sized</span> + <span class="pl-smi">FnMut</span><span class="pl-kos">(</span><span class="pl-kos">)</span> + <span class="pl-c1">'</span><span class="pl-ent">static</span><span class="pl-kos">&gt;</span> <span class="pl-smi">Deref</span> <span class="pl-k">for</span> <span class="pl-smi">S</span><span class="pl-kos">&lt;</span><span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">,</span> <span class="pl-smi">T</span><span class="pl-kos">&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">type</span> <span class="pl-smi">Target</span> = <span class="pl-smi">FnMut</span><span class="pl-kos">(</span><span class="pl-kos">)</span> + <span class="pl-c1">'</span><span class="pl-ent">a</span><span class="pl-kos">;</span>
    <span class="pl-k">fn</span> <span class="pl-en">deref</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">)</span> -&gt; <span class="pl-c1">&amp;</span><span class="pl-smi">Self</span><span class="pl-kos">::</span><span class="pl-smi">Target</span> <span class="pl-kos">{</span> <span class="pl-c1">&amp;</span><span class="pl-smi">self</span><span class="pl-kos">.</span><span class="pl-c1">0</span> <span class="pl-kos">}</span>
<span class="pl-kos">}</span>
<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> f = ||<span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> <span class="pl-k">mut</span> s = <span class="pl-v">S</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span><span class="pl-k">mut</span> f<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-en">s</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Yields:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;anon&gt;:13:5: 13:6 error: cannot borrow immutable borrowed content as mutable
&lt;anon&gt;:13     s();
              ^"><pre class="notranslate"><code class="notranslate">&lt;anon&gt;:13:5: 13:6 error: cannot borrow immutable borrowed content as mutable
&lt;anon&gt;:13     s();
              ^
</code></pre></div>
<p dir="auto">Auto-deref should be picking DerefMut here, not Deref.</p>