Go from Vec<&str> to Vec<f64>?

I have the following so far:

let values: Vec<Result<f64, std::num::ParseFloatError>> =
    parts.map(|value| value.parse::<f64>()).collect();

But what I really want is a Result<Vec<f64>, std::num::ParseFloatError> - and for it to return early if it does fail to parse any of the values.

1 Like

Ask, and you shall receive! Just change the type of values (playground):

    let values: Result<Vec<f64>, std::num::ParseFloatError> =
        parts.map(|value| value.parse::<f64>()).collect();

This works because Result implements FromIterator.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.