0
votes

I have a code that try to read a database. The get_f64 function is a third party function that reads a database and returns Result<f64,ValueAccessError> type.

It returns an error if there are no foo field inside my_variable. I need to convert it into Option<f64> it will return Some(f64) if there's a value and None if there's none/null. How to convert Result<T,E> into Option<T>, and return None for any error?

// this returned Result<f64, ValueAccessError>
let value = my_variable.get_f64("foo");

// but I need Option<f64>
1

1 Answers

2
votes

You can use Result::ok() to convert a Result into an Option:

let value = my_variable.get_f64("foo").ok();