The value returned from T::bar
has the 'static
lifetime, so Test2::foo
scope doesn't need to own anything. Returning &[T::bar()]
as &'static [&'static StructType]
should be safe? Test:foo
compiles without issue so I was expecting Test2::foo
to compile as well.
Code
pub struct StructType {
a: &'static str,
}
pub trait Foo {
fn foo() -> &'static [&'static StructType];
fn bar() -> &'static StructType;
}
pub struct Test;
impl Foo for Test {
fn foo() -> &'static [&'static StructType] {
&[&StructType { a: "asdf" }]
}
fn bar() -> &'static StructType {
&StructType { a: "asdf" }
}
}
pub struct Test2<T: Foo>(T);
impl<T: Foo> Test2<T> {
pub fn foo() -> &'static [&'static StructType] {
&[T::bar()]
}
}
Error
error[E0515]: cannot return reference to temporary value
--> src/lib.rs:26:9
|
26 | &[T::bar()]
| ^----------
| ||
| |temporary value created here
| returns a reference to data owned by the current function