/// The `Option` type. See [the module level documentation](index.html) for more.
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
+#[rustc_diagnostic_item = "option_type"]
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Option<T> {
/// No value
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_some(&self) -> bool {
- match *self {
- Some(_) => true,
- None => false,
- }
+ matches!(*self, Some(_))
}
/// Returns `true` if the option is a [`None`] value.
/// x.expect("the world is ending"); // panics with `the world is ending`
/// ```
#[inline]
+ #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn expect(self, msg: &str) -> T {
match self {
/// assert_eq!(x.unwrap(), "air"); // fails
/// ```
#[inline]
+ #[track_caller]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn unwrap(self) -> T {
match self {
/// }
/// ```
#[inline]
+ #[track_caller]
#[unstable(feature = "option_expect_none", reason = "newly added", issue = "62633")]
pub fn expect_none(self, msg: &str) {
if let Some(val) = self {
/// }
/// ```
#[inline]
+ #[track_caller]
#[unstable(feature = "option_unwrap_none", reason = "newly added", issue = "62633")]
pub fn unwrap_none(self) {
if let Some(val) = self {
// This is a separate function to reduce the code size of .expect() itself.
#[inline(never)]
#[cold]
+#[track_caller]
fn expect_failed(msg: &str) -> ! {
panic!("{}", msg)
}
// This is a separate function to reduce the code size of .expect_none() itself.
#[inline(never)]
#[cold]
+#[track_caller]
fn expect_none_failed(msg: &str, value: &dyn fmt::Debug) -> ! {
panic!("{}: {:?}", msg, value)
}