Harshit
Harshit

Reputation: 5157

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

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.

How can I achieve this?

Upvotes: 3

Views: 1287

Answers (1)

Harrison
Harrison

Reputation: 156

Check out the documentation for collect here.

Your struct RunResult should implement FromIterator<RunDetails>.

Try adding this (untested by me):

impl FromIterator<RunDetails> for RunResult {
    fn from_iter<I: IntoIterator<Item=RunDetails>>(iter: I) -> Self {
        Self {
            runs: Vec::from_iter(iter);
        }
    }
}

Upvotes: 2

Related Questions