-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Tracking Issue for Option::zip
and Option::zip_with
(feature option_zip
)
#70086
Comments
For what it's worth: my personal two cents are that both should be included, as even if Edit: One note is that Edit 2: Is there a better place for this discussion, or is here okay? |
Yeah, I've just found the same thing. E.g.: let a: Option<i32> = ...;
let b: Option<i32> = ...;
let res = a.zip_with(b, Add::add);
// or
let res = a.zip(b).map(|(a, b)| a + b); // destruction needed I think in this example |
I opened a PR to stabilize |
I was originally looking for this, but I feel like the upcoming let a = Some(7);
let b = Some(true);
let out = a.iter().zip(b).map(|(a, b)| s * o as u8).next(); //stable
let out = a.zip(b).map(|(a, b)| a * b as u8); // #![feature(option_zip)]
let out = a.zip_with(b, |a, b| a * b as u8); // #![feature(option_zip)]
let out: Option<_> = try { a? * b? as u8 }; // #![feature(try_blocks)] wow, such expressivity! let res = a.zip_with(b, Add::add);
let res = try { a? + b? };
wants_tuple(a.iter().zip(b).next()); //stable
wants_tuple(a.zip(b)); // #![feature(option_zip)]
wants_tuple(try { (a? * b?) }); // #![feature(try_blocks)] |
One interesting note is that Maybe we should also implement smt like this: fn zip_lazy(self, f: impl FnOnce() -> Option<U>) -> Option<T, U> { ... } ? 🤔 |
Stabilize Option::zip This PR stabilizes the following API: ```rust impl<T> Option<T> { pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)>; } ``` This API has real world usage as seen in <https://grep.app/search?q=-%3E%20Option%3C%5C%28T%2C%5Cs%3FU%5C%29%3E®exp=true&filter[lang][0]=Rust>. The `zip_with` method is left unstably as this API is kinda niche and it hasn't received much usage in Rust repositories on GitHub. cc rust-lang#70086
Stabilize Option::zip This PR stabilizes the following API: ```rust impl<T> Option<T> { pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)>; } ``` This API has real world usage as seen in <https://grep.app/search?q=-%3E%20Option%3C%5C%28T%2C%5Cs%3FU%5C%29%3E®exp=true&filter[lang][0]=Rust>. The `zip_with` method is left unstably as this API is kinda niche and it hasn't received much usage in Rust repositories on GitHub. cc rust-lang#70086
Stabilize Option::zip This PR stabilizes the following API: ```rust impl<T> Option<T> { pub fn zip<U>(self, other: Option<U>) -> Option<(T, U)>; } ``` This API has real world usage as seen in <https://grep.app/search?q=-%3E%20Option%3C%5C%28T%2C%5Cs%3FU%5C%29%3E®exp=true&filter[lang][0]=Rust>. The `zip_with` method is left unstably as this API is kinda niche and it hasn't received much usage in Rust repositories on GitHub. cc rust-lang#70086
There's one use case, suppose |
@lebensterben do you mean foo.as_mut().map(|first| Some(first.append(&mut bar?)));
// or
let _: Option<_> = try{ foo.as_mut()?.append(&mut bar?) }; |
I really like the interface haskell's lift functions have liftA :: Applicative f => (a -> b) -> f a -> f b
liftA3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA2 :: (a -> b -> c) -> f a -> f b -> f c Maybe we can have something similar in Rust Option::lift(f, optional1)
Option::lift2(f, optional1, optiona2)
// etc Probably the same interface for all applicatives (Iterators, Result, etc). |
Would |
@sollyucko yeah! If we consider doing it the number up to which we should have these functions is debatable. |
Required by the use of Option::zip. error[E0658]: use of unstable library feature 'option_zip' --> src/loc.rs:551:14 | 551 | .zip(self.expansion_loc.as_ref()) | ^^^ | = note: see issue #70086 <rust-lang/rust#70086> for more information error[E0658]: use of unstable library feature 'option_zip' --> src/loc.rs:529:36 | 529 | spelling_included_from.zip(expansion_included_from).map_or( | ^^^ | = note: see issue #70086 <rust-lang/rust#70086> for more information
We have quite many calls to |
let a = try { x? + y? };
let b = try { x?.checked_add(y?)? }; |
Hell yeah another useful utility function locked behind |
@LoganDark fyi I got under the impression that T-libs did not want to stabilize |
oh, it is? I accidentally autocompleted |
This is a tracking issue for
#![feature(option_zip)]
which was introduced in #69997.About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
Option::zip
(Stabilize Option::zip #72938)Unresolved Questions
zip_with
in the public API? Seems like a niche API and is easily replaced witha.zip(b).map(f)
.The text was updated successfully, but these errors were encountered: