-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Added Option::reduce #87036
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
Added Option::reduce #87036
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1049,6 +1049,47 @@ impl<T> Option<T> { | |
| { | ||
| Some(f(self?, other?)) | ||
| } | ||
|
|
||
| /// Zips `self` and another `Option` with function `f`, or returns `self.or(other)`. | ||
| /// | ||
| /// If `self` is `Some(s)` and `other` is `Some(o)`, this method returns `Some(f(s, o))`. | ||
| /// Otherwise, if `self` is `Some`, `self` is returned. | ||
| /// Otherwise, `other` is returned. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(option_or_zip_with)] | ||
| /// | ||
| /// #[derive(Debug, PartialEq)] | ||
| /// struct Point { | ||
| /// x: f64, | ||
| /// y: f64, | ||
| /// } | ||
| /// | ||
| /// impl Point { | ||
| /// fn new(x: f64, y: f64) -> Self { | ||
| /// Self { x, y } | ||
| /// } | ||
| /// } | ||
zesterer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// let x = Some(17.5); | ||
| /// let y = Some(42.7); | ||
zesterer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// assert_eq!(Some(2).or_zip_with(Some(3), |a, b| a + b), Some(5)); | ||
| /// assert_eq!(Some(2).or_zip_with(None, |a, b| a + b), Some(2)); | ||
| /// assert_eq!(None.or_zip_with(Some(3), |a, b| a + b), Some(3)); | ||
|
||
| /// ``` | ||
| #[unstable(feature = "option_or_zip_with", issue = "70086")] | ||
| pub fn or_zip_with<F>(self, other: Option<T>, f: F) -> Option<T> | ||
| where | ||
| F: FnOnce(T, T) -> T, | ||
| { | ||
| match (self, other) { | ||
| (Some(a), Some(b)) => Some(f(a, b)), | ||
| (a, b) => a.or(b), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T: Copy> Option<&T> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.