Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Add for Option<T> #75863

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ use crate::iter::{FromIterator, FusedIterator, TrustedLen};
use crate::pin::Pin;
use crate::{
convert, fmt, hint, mem,
ops::{self, Deref, DerefMut},
ops::{self, Add, Deref, DerefMut},
};

/// The `Option` type. See [the module level documentation](self) for more.
Expand Down Expand Up @@ -1260,6 +1260,19 @@ impl<T> Default for Option<T> {
}
}

#[stable(feature = "option_add", since = "1.46.0")]
impl<T: Add<Output = T>> Add for Option<T> {
type Output = Self;

fn add(self, other: Self) -> Self::Output {
match (self, other) {
(a, None) => a,
(None, b) => b,
Comment on lines +1269 to +1270
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential footgun, it may not be obvious that None is ignored, I thought None should become None at first if either one is None.

(Some(a), Some(b)) => Some(a.add(b)),
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> IntoIterator for Option<T> {
type Item = T;
Expand Down