Open
Description
Hi, here is an example of very strange error I have stuck with:
struct Baz {
pub foo: &'static str
}
trait Foo {
fn foo() -> &'static str;
}
trait Bar {
fn bar() -> &'static [Baz];
}
struct Test;
impl Foo for Test {
fn foo() -> &'static str {
"Hello, World!"
}
}
struct Bat {
test: Test
}
impl Bar for Bat {
fn bar() -> &'static [Baz] {
use Foo;
&[
/// This compiles
{
Baz { foo: "Hello, World!" }
},
/// but, this cause an error "cannot return reference to temporary value"
{
let foo = "Hello, World!";
Baz { foo }
},
/// this cause an error, also
{
let foo = Test::foo();
Baz { foo }
},
/// and this cause an error, too
{
Baz { foo: Test::foo() }
}
]
}
}
and the error message:
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:29:9
|
29 | &[
| __________^-
| | _________|
| ||
30 | || /// This compiles
31 | || {
32 | || Baz { foo: "Hello, World!" }
... ||
50 | || }
51 | || ]
| || ^
| ||_________|
| |__________returns a reference to data owned by the current function
| temporary value created here