<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="use std::io;

fn make_proc() -&gt; proc() {
    let s = ~&quot;foo&quot;;
    proc() {
        drop(s);
        io::stderr().write_str(s).unwrap();
    }
}

fn main() {
    let p = make_proc();
    p();
}"><pre lang=".rs" class="notranslate"><code class="notranslate">use std::io;

fn make_proc() -&gt; proc() {
    let s = ~"foo";
    proc() {
        drop(s);
        io::stderr().write_str(s).unwrap();
    }
}

fn main() {
    let p = make_proc();
    p();
}
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ rustc -v
rustc 0.10-pre (68a4f7d 2014-02-24 12:42:02 -0800)
host: x86_64-unknown-linux-gnu

$ rustc foo.rs
$ ./foo
Segmentation fault"><pre class="notranslate"><code class="notranslate">$ rustc -v
rustc 0.10-pre (68a4f7d 2014-02-24 12:42:02 -0800)
host: x86_64-unknown-linux-gnu

$ rustc foo.rs
$ ./foo
Segmentation fault
</code></pre></div>
<p dir="auto">(Apologies if this has already been fixed; I'm using Servo's <code class="notranslate">rustc</code>.)</p>
<h1 dir="auto">Updated bug</h1>
<p dir="auto">The compiler allows this code when it shouldn't</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="fn main() {
    let a = ~&quot;&quot;;
    let b: ~[&amp;str] = a.lines().collect();
    drop(a);
    for s in b.iter() {
        println!(&quot;{}&quot;, *s);
    }
}"><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> a = ~""<span class="pl-kos">;</span>
    <span class="pl-k">let</span> b<span class="pl-kos">:</span> ~<span class="pl-kos">[</span><span class="pl-c1">&amp;</span><span class="pl-smi">str</span><span class="pl-kos">]</span> = a<span class="pl-kos">.</span><span class="pl-en">lines</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">collect</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-en">drop</span><span class="pl-kos">(</span>a<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">for</span> s <span class="pl-k">in</span> b<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-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"{}"</span>, *s<span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<h1 dir="auto">Original description</h1>
<p dir="auto">The following code is buggy:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="let foo = ~&quot;hello&quot;;
let foo: ~[&amp;str] = foo.words().collect();
let invalid_string = foo[0];"><pre class="notranslate"><span class="pl-k">let</span> foo = ~"hello"<span class="pl-kos">;</span>
<span class="pl-k">let</span> foo<span class="pl-kos">:</span> ~<span class="pl-kos">[</span><span class="pl-c1">&amp;</span><span class="pl-smi">str</span><span class="pl-kos">]</span> = foo<span class="pl-kos">.</span><span class="pl-en">words</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">collect</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">let</span> invalid_string = foo<span class="pl-kos">[</span><span class="pl-c1">0</span><span class="pl-kos">]</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Invalid string contains, as the name suggests, an invalid string; sometimes I get a string filled with <code class="notranslate">\x00</code>, sometimes just garbage, and usually it will eventually assert that the string contains invalid characters.</p>
<p dir="auto">It appears that the first <code class="notranslate">foo</code> gets dropped when the second one is assigned?<br>
Either the first <code class="notranslate">foo</code> is supposed to be dropped as happens now, and the borrow checker should forbid this code (since the old <code class="notranslate">foo</code> no longer exists, <code class="notranslate">&amp;str</code> cannot have a lifetime), or it should let the first <code class="notranslate">foo</code> live until the end of the current scope, and make the above work.</p>