Idiomatic convervsion of Option Type into different Option Type

I need to convert different Option Types from one type into another type.

E.g. Option<f64> to Option<String>.

What is the idiomatic way to do this?

My approach:

let f: Option<f64> = Some(4.);
let f_converted = match f {
        Some(el) => Some(el.to_string()),
        None => None,
    };

Is there a better / more idiomatic way to do this?

Thanks!

f_converted = f.map(|e| e.to_string());

3 Likes

Thanks!

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