我想保留一个Holded
实例,但我不能,因为它在一个数组中。如何从数组中“提取”此实例并将其保存在Object
实例中? (在我的原始代码中,我没有数组但是迭代器)。这是等效的代码:
struct Holded {
value: u8,
}
struct Holder;
impl Holder {
pub fn get(&self) -> [Holded; 2] {
[Holded { value: 0 }, Holded { value: 1 }]
}
}
struct Object<'a> {
holded: &'a Holded,
}
fn main() {
let holder = Holder;
let obj = work(&holder).unwrap();
println!("{}", obj.holded.value);
}
fn work(holder: &Holder) -> Option<Object> {
let mut obj: Object;
let array = holder.get();
for h in array.into_iter() {
if h.value == 1u8 {
obj = Object { holded: h };
return Some(obj);
}
}
None
}
错误消息:
error: `array` does not live long enough
--> src/main.rs:28:14
|
28 | for h in array.into_iter() {
| ^^^^^ does not live long enough
...
36 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the block at 24:43...
--> src/main.rs:24:44
|
24 | fn work(holder: &Holder) -> Option<Object> {
| ^
答案 0 :(得分:0)
在您发布的MCVE中,结构Object
包含对Holded
的引用:
struct Object<'a> {
holded: &'a Holded,
}
在函数work()
中,您返回Object
(可选):
fn work(holder: &Holder) -> Option<Object> {
您从一个按值返回的函数中获取Holded
:
impl Holder {
pub fn get( &self ) -> [Holded; 2] {
[Holded { value: 0 }, Holded { value: 1 }]
}
}
现在这将永远不会奏效。如果您返回对Holded
的引用,则您引用的Holded
必须存储在某处。这意味着要么作为输入,要么作为函数work()
的输出。
我重写了您的示例,将Holded
包含在Holder
中。这是解决这个问题的一种方法。但我不确定这是否适用于您原来的问题。
struct Holded {
value: u8,
}
struct Holder{
value: [Holded; 2],
}
impl Holder {
pub fn new() -> Holder {
Holder {value: [Holded { value: 0 }, Holded { value: 1 }] }
}
pub fn get( &self ) -> &[Holded; 2] {
&self.value
}
}
struct Object<'a> {
holded: &'a Holded,
}
fn main() {
let holder = Holder::new();
let obj = work(&holder).unwrap();
println!("{}", obj.holded.value);
let obj = work2(&holder).unwrap();
println!("{}", obj.holded.value);
}
fn work(holder: &Holder) -> Option<Object> {
let obj: Object;
let array = holder.get();
for h in array.into_iter() {
if h.value == 1u8 {
obj = Object { holded: h };
return Some(obj);
}
}
None
}
fn work2(holder: &Holder) -> Option<Object> {
holder.get()
.iter()
.filter(|h| h.value == 1u8)
.map(|h| Object { holded: h })
.next()
}
您注意到我还添加了一种不同的方式来实现work()
函数(work2()
)。