[英]Rust - value of type cannot be built from `std::iter::Iterator<>`
I have struct from which I want to extract the data.我有要从中提取数据的结构。
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunResult {
pub runs: Vec<RunDetails>,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct RunDetails {
pub details: String,
pub name: String,
pub id: String,
pub type: String,
}
I am getting the data which got inserted in above struct in the variable result
which I am iterating below我在下面迭代的变量
result
获取插入到上面结构中的数据
let my_result:RunResult = result
.runs
.into_iter()
.filter(|line| line.details.contains(&"foo"))
.collect();
I am getting the error as我收到的错误是
value of type `RunResult` cannot be built from `std::iter::Iterator<Item=RunDetails>`
I want to get the record where type=OK
, details contains foo
with highest id
.我想获取
type=OK
的记录, details contains foo
具有最高id
details contains foo
。
How can I achieve this?我怎样才能做到这一点?
Check out the documentation for collect
here . 在此处查看
collect
的文档。
Your struct RunResult
should implement FromIterator<RunDetails>
.您的 struct
RunResult
应该实现FromIterator<RunDetails>
。
Try adding this:尝试添加这个:
impl FromIterator<RunDetails> for RunResult {
fn from_iter<I: IntoIterator<Item=RunDetails>>(iter: I) -> Self {
let v = Vec::new();
for r in iter {
v.push(r);
}
Self {
runs: v
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.