简体   繁体   English

Rust - 类型的值不能从 `std::iter::Iterator<>` 构建

[英]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.

相关问题 `Vec&lt;&amp;str&gt;` 类型的值不能从`std::iter::Iterator 构建<item=&&str> `</item=&&str> - Value of type `Vec<&str>` cannot be built from `std::iter::Iterator<Item=&&str>` 不能从 `std::iter::Iterator 构建 `str` 类型的集合<Item=char> ` - a collection of type `str` cannot be built from `std::iter::Iterator<Item=char>` 将(迭代器)值传递给闭包(预期类型参数`I`,找到结构`std::slice::Iter`)有什么问题? - What is the issue with passing an (iterator) value into a closure (expected type parameter `I`, found struct `std::slice::Iter`)? 预期 std::iter::Iterator,但找到 std::iter::Iterator - Expected std::iter::Iterator, but std::iter::Iterator found 在 Rust 中,为什么 std::iter::Iterator 的 min 函数返回一个引用? - In Rust, why does std::iter::Iterator's min function return a reference? 如何在 Rust 中将 std::iter::Iterator::map 用于树状结构? - How to use std::iter::Iterator::map for tree-like structures in Rust? Vec&lt;&amp;str&gt;` 不能从 () 类型的元素上的迭代器构建 - Vec<&str>` cannot be built from an iterator over elements of type () 在Rust中使用map时,“无法推断出`_`”的类型 - “cannot infer type for `_`” when using map on iter in Rust 泛型类型不能从泛型元素上的迭代器构建 - Generic type cannot be built from iterator over generic elements HashMap 编辑值和迭代器 Rust - HashMap edit value and Iter Rust
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM