<p dir="auto">Even if this example may look silly, it's the result of the simplification of a more complex structure.<br>
There should be no reason for these constructs not to compile, should it ?</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="use std::sync::RwLock;

fn main() {
    let a = RwLock::new(RwLock::new(String::new()));

    // Fails : a does not live long enough
    let len = a.read().unwrap().read().unwrap().len();

    // Fails : b does not live long enough
    let len = { let b = a.read().unwrap(); b.read().unwrap().len() };

    // Compiles
    let len = { let b = a.read().unwrap(); let c = b.read().unwrap(); c.len() };
}"><pre class="notranslate"><span class="pl-k">use</span> std<span class="pl-kos">::</span>sync<span class="pl-kos">::</span><span class="pl-v">RwLock</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> a = <span class="pl-smi">RwLock</span><span class="pl-kos">::</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-smi">RwLock</span><span class="pl-kos">::</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-smi">String</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-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">// Fails : a does not live long enough</span>
    <span class="pl-k">let</span> len = a<span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">len</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

    <span class="pl-c">// Fails : b does not live long enough</span>
    <span class="pl-k">let</span> len = <span class="pl-kos">{</span> <span class="pl-k">let</span> b = a<span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> b<span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">len</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">}</span><span class="pl-kos">;</span>

    <span class="pl-c">// Compiles</span>
    <span class="pl-k">let</span> len = <span class="pl-kos">{</span> <span class="pl-k">let</span> b = a<span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-k">let</span> c = b<span class="pl-kos">.</span><span class="pl-en">read</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">unwrap</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span> c<span class="pl-kos">.</span><span class="pl-en">len</span><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">The full error message for the first construct does not really show what the problem might be:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;anon&gt;:5:15: 5:32 error: borrowed value does not live long enough
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
                       ^~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 note: reference must be valid for the destruction scope surrounding statement at 5:4...
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 note: ...but borrowed value is only valid for the statement at 5:4
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 help: consider using a `let` binding to increase its lifetime
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error"><pre class="notranslate"><code class="notranslate">&lt;anon&gt;:5:15: 5:32 error: borrowed value does not live long enough
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
                       ^~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 note: reference must be valid for the destruction scope surrounding statement at 5:4...
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 note: ...but borrowed value is only valid for the statement at 5:4
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&lt;anon&gt;:5:5: 5:54 help: consider using a `let` binding to increase its lifetime
&lt;anon&gt;:5     let len = a.read().unwrap().read().unwrap().len();
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
</code></pre></div>
<p dir="auto">Spawned off of  <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="54076219" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/21022" data-hovercard-type="pull_request" data-hovercard-url="/rust-lang/rust/pull/21022/hovercard" href="https://github.com/rust-lang/rust/pull/21022">#21022</a>, <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="56708388" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/21972" data-hovercard-type="pull_request" data-hovercard-url="/rust-lang/rust/pull/21972/hovercard" href="https://github.com/rust-lang/rust/pull/21972">#21972</a>; see also <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="55493878" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/21657" data-hovercard-type="pull_request" data-hovercard-url="/rust-lang/rust/pull/21657/hovercard" href="https://github.com/rust-lang/rust/pull/21657">#21657</a>.</p>
<p dir="auto">Tracking order of destruction for r-value temporaries would (probably) allow us to resume writing code like this from <a href="https://github.com/rust-lang/rfcs/blob/master/text/0769-sound-generic-drop.md#some-temporaries-need-to-be-given-names">RFC 769</a>:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="for line in old_io::stdin().lock().lines() {
    ...
}"><pre class="notranslate"><span class="pl-k">for</span> line <span class="pl-k">in</span> old_io<span class="pl-kos">::</span><span class="pl-en">stdin</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">lock</span><span class="pl-kos">(</span><span class="pl-kos">)</span><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-kos">.</span><span class="pl-c1"></span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">which is currently disallowed because the region inference system does not know that the temporary yielded by <code class="notranslate">stdin()</code> will outlive the temporary from <code class="notranslate">stdin().lock()</code>.</p>
<p dir="auto">(But note that it might also have fallout, potentially worse than that of <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="55493878" data-permission-text="Title is private" data-url="https://github.com/rust-lang/rust/issues/21657" data-hovercard-type="pull_request" data-hovercard-url="/rust-lang/rust/pull/21657/hovercard" href="https://github.com/rust-lang/rust/pull/21657">#21657</a>)</p>