Created
April 1, 2022 04:51
-
-
Save hussachai/b6e13a57fca3636c84fd1d6d43598a8e to your computer and use it in GitHub Desktop.
Error Handling in Rust that Every Beginner should Know Snippet 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub const fn unwrap(self) -> T { | |
match self { | |
Some(val) => val, | |
None => panic!("called `Option::unwrap()` on a `None` value"), | |
} | |
} | |
pub fn expect(self, msg: &str) -> T { | |
match self { | |
Some(val) => val, | |
None => expect_failed(msg), | |
} | |
} | |
fn expect_failed(msg: &str) -> ! { | |
panic!("{}", msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment