Uh oh!
There was an error while loading. Please reload this page.
add Option::{zip,zip_with} methods under "option_zip" gate - #69997
Conversation
rust-highfive
commented
Mar 14, 2020
(rust_highfive has picked a reviewer for you, use r? to override) |
Centril
commented
Mar 14, 2020
|
WaffleLapkin
commented
Mar 14, 2020
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 nightlylet tuple:(Option<A>,Option<B>) = ...;let res:Option<(A,B)> = try {(tuple.0?, tuple.1?)} |
WaffleLapkin
commented
Mar 14, 2020
Hmmm, |
Centril
commented
Mar 14, 2020
I'm in favor of adding I'm less sure about |
bors
commented
Mar 17, 2020
☔ 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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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`.
WaffleLapkin
commented
Mar 18, 2020
If |
- remove `#[inline]` attributes (see rust-lang#69997 (comment)) - fill tracking issue in `#[unstable]` attributes - slightly improve the docs
LukasKalbertodt
commented
Mar 18, 2020
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.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
LukasKalbertodt
commented
Mar 20, 2020
Thanks! @bors r+ |
bors
commented
Mar 20, 2020
📌 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.