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

add Option::{zip,zip_with} methods under "option_zip" gate #69997

Merged
merged 3 commits into from
Mar 21, 2020

Commits on Mar 18, 2020

  1. add Option::{zip,zip_with} methods under "option_zip" gate

    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 committed Mar 18, 2020
    Configuration menu
    Copy the full SHA
    a5206f9 View commit details
    Browse the repository at this point in the history
  2. fixes to Option::{zip,zip_with}

    - remove `#[inline]` attributes (see rust-lang#69997 (comment))
    - fill tracking issue in `#[unstable]` attributes
    - slightly improve the docs
    WaffleLapkin committed Mar 18, 2020
    Configuration menu
    Copy the full SHA
    d36d3fa View commit details
    Browse the repository at this point in the history

Commits on Mar 19, 2020

  1. Configuration menu
    Copy the full SHA
    121bffc View commit details
    Browse the repository at this point in the history