Skip to content

Latest commit

 

History

History
18 lines (13 loc) · 375 Bytes

option-to-result-rust.org

File metadata and controls

18 lines (13 loc) · 375 Bytes

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"));