Open
Description
use std::ops::Index;
fn main() {
let _sugar = &"a".to_owned()[..];
let _desugar1 = "a".to_owned().index(..);
let _desugar2 = &*"a".to_owned().index(..);
}
<anon>:5:21: 5:35 error: borrowed value does not live long enough
<anon>:5 let _desugar1 = "a".to_owned().index(..);
^~~~~~~~~~~~~~
<anon>:5:46: 6:49 note: reference must be valid for the block suffix following statement 1 at 5:45...
<anon>:5 let _desugar1 = "a".to_owned().index(..);
<anon>:6 let _desugar2 = &*"a".to_owned().index(..);}
<anon>:5:5: 5:46 note: ...but borrowed value is only valid for the statement at 5:4
<anon>:5 let _desugar1 = "a".to_owned().index(..);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:5:5: 5:46 help: consider using a `let` binding to increase its lifetime
<anon>:5 let _desugar1 = "a".to_owned().index(..);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:6:23: 6:37 error: borrowed value does not live long enough
<anon>:6 let _desugar2 = &*"a".to_owned().index(..);}
^~~~~~~~~~~~~~
<anon>:6:48: 6:49 note: reference must be valid for the block suffix following statement 2 at 6:47...
<anon>:6 let _desugar2 = &*"a".to_owned().index(..);}
^
<anon>:6:5: 6:48 note: ...but borrowed value is only valid for the statement at 6:4
<anon>:6 let _desugar2 = &*"a".to_owned().index(..);}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:6:5: 6:48 help: consider using a `let` binding to increase its lifetime
<anon>:6 let _desugar2 = &*"a".to_owned().index(..);}
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the let _desugar
s are commented out, it compiles fine.
It seems a method call is treated differently to the []
syntax. This may be a purposeful consequence of the design of temporary lifetimes, I don't know.