add Option::{zip,zip_with} methods under "option_zip" gate#69997
add Option::{zip,zip_with} methods under "option_zip" gate#69997bors merged 3 commits intorust-lang:masterfrom
Option::{zip,zip_with} methods under "option_zip" gate#69997Conversation
|
(rust_highfive has picked a reviewer for you, use r? to override) |
|
|
Other currently possible solutions: let tuple: (Option<A>, Option<B>) = ...;
let res: Option<(A, B)> = match tuple {
(Some(a), Some(b)) => Some((a, b)),
_ => None,
};#![feature(try_blocks)] // require nightly
let tuple: (Option<A>, Option<B>) = ...;
let res: Option<(A, B)> = try { (tuple.0?, tuple.1?) } |
Hmmm, |
|
I'm in favor of adding I'm less sure about |
|
☔ The latest upstream changes (presumably #70062) made this pull request unmergeable. Please resolve the merge conflicts. |
LukasKalbertodt
left a comment
There was a problem hiding this comment.
Thanks a bunch for the PR!
I agree with @Centril and am also in favor of adding Option::zip. I think I'd prefer not to add Option::zip_with though. I think it's pretty niche. But I'm fine with merging it as unstable to find out if users find good uses for it.
I added a few line comments, but nothing serious. And you need to rebase.
This commit introduces 2 methods - `Option::zip` and `Option::zip_with` with respective signatures: - zip: `(Option<T>, Option<U>) -> Option<(T, U)>` - zip_with: `(Option<T>, Option<U>, (T, U) -> R) -> Option<R>` Both are under the feature gate "option_zip". I'm not sure about the name "zip", maybe we can find a better name for this. (I would prefer `union` for example, but this is a keyword :( ) -------------------------------------------------------------------------------- Recently in a russian rust begginers telegram chat a newbie asked (translated): > Are there any methods for these conversions: > > 1. `(Option<A>, Option<B>) -> Option<(A, B)>` > 2. `Vec<Option<T>> -> Option<Vec<T>>` > > ? While second (2.) is clearly `vec.into_iter().collect::<Option<Vec<_>>()`, the first one isn't that clear. I couldn't find anything similar in the `core` and I've come to this solution: ```rust let tuple: (Option<A>, Option<B>) = ...; let res: Option<(A, B)> = tuple.0.and_then(|a| tuple.1.map(|b| (a, b))); ``` However this solution isn't "nice" (same for just `match`/`if let`), so I thought that this functionality should be in `core`.
fbec5cd to
b79e964
Compare
b79e964 to
8ec4823
Compare
|
If |
8ec4823 to
d36d3fa
Compare
- remove `#[inline]` attributes (see rust-lang#69997 (comment)) - fill tracking issue in `#[unstable]` attributes - slightly improve the docs
I don't have a strong preference, but I'd say you can keep it as is for now. I suspect that when we are discussing stabilizing |
LukasKalbertodt
left a comment
There was a problem hiding this comment.
Just a tiny thing still.
|
Thanks! @bors r+ |
|
📌 Commit 121bffc has been approved by |
…bertodt
add `Option::{zip,zip_with}` methods under "option_zip" gate
This PR introduces 2 methods - `Option::zip` and `Option::zip_with` with
respective signatures:
- zip: `(Option<T>, Option<U>) -> Option<(T, U)>`
- zip_with: `(Option<T>, Option<U>, (T, U) -> R) -> Option<R>`
Both are under the feature gate "option_zip".
I'm not sure about the name "zip", maybe we can find a better name for this.
(I would prefer `union` for example, but this is a keyword :( )
--------------------------------------------------------------------------------
Recently in a russian rust begginers telegram chat a newbie asked (translated):
> Are there any methods for these conversions:
>
> 1. `(Option<A>, Option<B>) -> Option<(A, B)>`
> 2. `Vec<Option<T>> -> Option<Vec<T>>`
>
> ?
While second (2.) is clearly `vec.into_iter().collect::<Option<Vec<_>>()`, the
first one isn't that clear.
I couldn't find anything similar in the `core` and I've come to this solution:
```rust
let tuple: (Option<A>, Option<B>) = ...;
let res: Option<(A, B)> = tuple.0.and_then(|a| tuple.1.map(|b| (a, b)));
```
However this solution isn't "nice" (same for just `match`/`if let`), so I thought
that this functionality should be in `core`.
…bertodt
add `Option::{zip,zip_with}` methods under "option_zip" gate
This PR introduces 2 methods - `Option::zip` and `Option::zip_with` with
respective signatures:
- zip: `(Option<T>, Option<U>) -> Option<(T, U)>`
- zip_with: `(Option<T>, Option<U>, (T, U) -> R) -> Option<R>`
Both are under the feature gate "option_zip".
I'm not sure about the name "zip", maybe we can find a better name for this.
(I would prefer `union` for example, but this is a keyword :( )
--------------------------------------------------------------------------------
Recently in a russian rust begginers telegram chat a newbie asked (translated):
> Are there any methods for these conversions:
>
> 1. `(Option<A>, Option<B>) -> Option<(A, B)>`
> 2. `Vec<Option<T>> -> Option<Vec<T>>`
>
> ?
While second (2.) is clearly `vec.into_iter().collect::<Option<Vec<_>>()`, the
first one isn't that clear.
I couldn't find anything similar in the `core` and I've come to this solution:
```rust
let tuple: (Option<A>, Option<B>) = ...;
let res: Option<(A, B)> = tuple.0.and_then(|a| tuple.1.map(|b| (a, b)));
```
However this solution isn't "nice" (same for just `match`/`if let`), so I thought
that this functionality should be in `core`.
Rollup of 16 pull requests Successful merges: - rust-lang#65097 (Make std::sync::Arc compatible with ThreadSanitizer) - rust-lang#69033 (Use generator resume arguments in the async/await lowering) - rust-lang#69997 (add `Option::{zip,zip_with}` methods under "option_zip" gate) - rust-lang#70038 (Remove the call that makes miri fail) - rust-lang#70058 (can_begin_literal_maybe_minus: `true` on `"-"? lit` NTs.) - rust-lang#70111 (BTreeMap: remove shared root) - rust-lang#70139 (add delay_span_bug to TransmuteSizeDiff, just to be sure) - rust-lang#70165 (Remove the erase regions MIR transform) - rust-lang#70166 (Derive PartialEq, Eq and Hash for RangeInclusive) - rust-lang#70176 (Add tests for rust-lang#58319 and rust-lang#65131) - rust-lang#70177 (Fix oudated comment for NamedRegionMap) - rust-lang#70184 (expand_include: set `.directory` to dir of included file.) - rust-lang#70187 (more clippy fixes) - rust-lang#70188 (Clean up E0439 explanation) - rust-lang#70189 (Abi::is_signed: assert that we are a Scalar) - rust-lang#70194 (#[must_use] on split_off()) Failed merges: r? @ghost
Edit: Tracking issue
This PR introduces 2 methods -
Option::zipandOption::zip_withwithrespective signatures:
(Option<T>, Option<U>) -> Option<(T, U)>(Option<T>, Option<U>, (T, U) -> R) -> Option<R>Both are under the feature gate "option_zip".
I'm not sure about the name "zip", maybe we can find a better name for this.
(I would prefer
unionfor example, but this is a keyword :( )Recently in a russian rust begginers telegram chat a newbie asked (translated):
While second (2.) is clearly
vec.into_iter().collect::<Option<Vec<_>>(), thefirst one isn't that clear.
I couldn't find anything similar in the
coreand I've come to this solution:However this solution isn't "nice" (same for just
match/if let), so I thoughtthat this functionality should be in
core.