Option to Result in Rust

This is how you convert from Option to Result and vice versa in Rust .

Option -> Result

let err = anyhow!("some error");
let x = Some("foo");
assert_eq!(x.ok_or(err), Ok("foo"));

Result -> Option

let x = Ok("foo");
assert_eq!(x.ok(), Some("foo"));
Edit this page on GitHub