This rust code doesn't work. Is there any detailed explanation for why? It gives the error "cannot return reference to temporary value"
trait NonInherited {
fn get_type(&self) -> &str;
}
struct Inherited {
val: usize,
}
impl NonInherited for Inherited {
fn get_type(&self) -> &str {
return "inherited";
}
}
fn helper(list: &mut [usize; 2]) -> &'static dyn NonInherited {
// works
//return &Inherited {val: 3};
// does not work
return &Inherited {val: list[1]}; // Error is here
}
fn main() {
let mut a: [usize; 2] = [1, 7];
println!("{}", helper(&mut a).get_type());
}