rust - How to convert a Option<Result<T, Error>> to an Option<T> without unwrapping it? -


i trying find way convert option<string> option<i8>.

for example,

use std::str::fromstr;  fn main() {     let some_option: option<string> = some("too".to_owned());      let new_option: option<i8> = some_option.map(|x| i8::from_str(x.as_str()));  } 

i thought use turbo fish explicitly cast type this:

use std::str::fromstr;  fn main() {     let some_option: option<string> = some("too".to_owned());      let new_option: option<i8> = some_option.map::<option<i8>>(|x| i8::from_str(x.as_str()));  } 

however, compiler points out isn't correct amount of parameters, thought might work, doesn't:

use std::str::fromstr;  fn main() {     let some_option: option<string> = some("too".to_owned());      let new_option: option<i8> = some_option.map::<option<i8>,i8::from_str>();  } 

you can use ok() , unwrap_or() functions:

fn test() -> option<result<u32, ()>> {     some(ok(1)) }  fn main() {     let x: option<result<_, _>> = test();     println!("{:?}", x.map(|r| r.ok()).unwrap_or(none)); } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -