我有下面的代码,我试图返回 struct Foo
,其中包含字段values
的一组默认值.这些值可能会在以后更改.但编者抱怨:
error: `initial` does not live long enough
如何做到这一点?还有别的 Select 吗?
struct Foo <'a> {
values: &'a mut Vec<i32>,
}
impl <'a> Foo <'a> {
fn new() -> Foo <'a> {
let initial = vec![1, 2];
Foo { values: &mut initial }
}
}
let my_foo = Foo::new();
my_foo.values.push(3);