<p dir="auto">Following code passes borrow checking leading to serious memory unsafety.</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="fn main() {
    let mut v = Vec::new();
    for _ in v.iter() {
        v.push('j');
    }
}"><pre class="notranslate"><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> v = <span class="pl-smi">Vec</span><span class="pl-kos">::</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">for</span> _ <span class="pl-k">in</span> v<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>
        v<span class="pl-kos">.</span><span class="pl-en">push</span><span class="pl-kos">(</span><span class="pl-s">'j'</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">Code compiles with <code class="notranslate">&amp;c</code> instead of <code class="notranslate">_</code> too. Loop fails to compile only in <code class="notranslate">for c in v.iter()</code> case where there's an explicit variable with reference or when the loop is desugared manually.</p>
<p dir="auto">This simple code compiles on current master (<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/rust-lang/rust/commit/0d3bd7720c50e3ada4bac77331d43926493be4fe/hovercard" href="https://github.com/rust-lang/rust/commit/0d3bd7720c50e3ada4bac77331d43926493be4fe"><tt>0d3bd77</tt></a>) :</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="fn main() {
    let mut vector = vec!(1u, 2u);
    for &amp;x in vector.iter() {
        let cap = vector.capacity();
        println!(&quot;Capacity was: {}&quot;, cap);
        vector.grow(cap, &amp;0u); // be sure to cause reallocation
        *vector.get_mut(1u) = 5u;
        println!(&quot;Value: {}&quot;, x);
    }
}"><pre class="notranslate"><code class="notranslate">fn main() {
    let mut vector = vec!(1u, 2u);
    for &amp;x in vector.iter() {
        let cap = vector.capacity();
        println!("Capacity was: {}", cap);
        vector.grow(cap, &amp;0u); // be sure to cause reallocation
        *vector.get_mut(1u) = 5u;
        println!("Value: {}", x);
    }
}
</code></pre></div>
<p dir="auto">and outputs:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Capacity was: 4
Value: 1
Capacity was: 8
Value: 2"><pre class="notranslate"><code class="notranslate">Capacity was: 4
Value: 1
Capacity was: 8
Value: 2
</code></pre></div>
<p dir="auto">while on 0.11 the borrow checker would complain that <code class="notranslate">vector</code> is already immutably borrowed by <code class="notranslate">vector.iter()</code>.<br>
It allows me to write <code class="notranslate">5u</code> in <code class="notranslate">vector[1]</code> and then read it as <code class="notranslate">2u</code>, because reallocation of the vector caused the iterator's slice to refer to freed memory.</p>
<p dir="auto">(was found in a StackOverflow question : <a href="http://stackoverflow.com/q/25528271/2536143" rel="nofollow">http://stackoverflow.com/q/25528271/2536143</a> )</p>