Skip to content

Instantly share code, notes, and snippets.

@hussachai
Created April 1, 2022 04:51
Show Gist options
  • Save hussachai/b6e13a57fca3636c84fd1d6d43598a8e to your computer and use it in GitHub Desktop.
Save hussachai/b6e13a57fca3636c84fd1d6d43598a8e to your computer and use it in GitHub Desktop.
Error Handling in Rust that Every Beginner should Know Snippet 1
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