gpt4 book ai didi

rust - 在 Rust 1.26 中推断存在类型(impl Trait)的生命周期

转载 作者:行者123 更新时间:2023-11-29 08:13:57 25 4
gpt4 key购买 nike

<分区>

我正在尝试重写一个使用存在类型的方法,但我在破译错误时遇到了问题:

use std::result;

pub struct Child {
pub value: u32,
}

pub struct Parent {
pub name: u32,
}

impl Parent {
pub fn process(&self, _l: &Child) -> result::Result<(), ()> {
Ok(())
}

pub fn convert(&self, l: &Child) {
()
}

pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
let x: Vec<_> = l.into_iter()
.map(|tr| self.process(&tr).map(|_| tr))
.collect(); // Calling collect() here forces all debits to complete

let y = x.into_iter().map(|d| {
d.map(|c| {
self.convert(&c);
c
})
});
y
}
}

fn main() {
let b = Parent { name: 0 };
let l = vec![Child { value: 10 }, Child { value: 20 }];
let _: Vec<Child> = b.looper(l).map(|x| x.unwrap()).collect();
}

我的错误消息指出:

error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:25:35
|
25 | let y = x.into_iter().map(|d| {
| ___________________________________^
26 | | d.map(|c| {
27 | | self.convert(&c);
28 | | c
29 | | })
30 | | });
| |_________^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 20:5...
--> src/main.rs:20:5
|
20 | / pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
21 | | let x: Vec<_> = l.into_iter()
22 | | .map(|tr| self.process(&tr).map(|_| tr))
23 | | .collect(); // Calling collect() here forces all debits to complete
... |
31 | | y
32 | | }
| |_____^
= note: ...so that the types are compatible:
expected &&Parent
found &&Parent
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that return value is valid for the call
--> src/main.rs:20:44
|
20 | pub fn looper(&self, l: Vec<Child>) -> impl Iterator<Item = Result<Child, ()>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com