Skip to content

Tracking issue: Provide Entry-like API for Option #39288

Closed

Description

@shahn

Often I have code where Option is used to indicate whether a value has been initialized or not. In these instances I often have code that either wants to use the value directly or initialize it and then use the value. So what I'm looking for is similar to as_ref()/as_mut() except they should return &T/&mut T respectively, and take either a value or a closure.

I think I would call the functions get{,_mut}_or_insert{,with}() marrying the get functions (for example in hashmaps) for getting references with the or_insert Entry like functions.

Activity

  1. Thinkofname commented on Jan 25, 2017

    @Thinkofname

    Isn't this just as_ref/mut().unwrap_or(val) and as_ref/mut().unwrap_or_else(|| val) ?

    Edit: These don't modify the original Option which I guess it what you wanted

  2. shahn commented on Jan 25, 2017

    @shahn
    ContributorAuthor

    indeed

  3. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    T-libs-api[DEPRECATED; DO NOT USE]
    on Feb 4, 2017
  4. added a commit that references this issue on Feb 4, 2017
  5. tbu- commented on Feb 6, 2017

    @tbu-
    Contributor

    See also #25149 and #29203. Also, this seems fixed by #39289.

  6. shahn commented on Feb 6, 2017

    @shahn
    ContributorAuthor

    AIUI, this is not fixed by #39289, because this issue is referenced as the tracking issue for the implementation and should stay open until it's decided whether this is to be stabilized or backed out.

  7. tbu- commented on Feb 6, 2017

    @tbu-
    Contributor

    Ah, right. Maybe you could put "Tracking issue" in the title.

  8. changed the title [-]Provide Entry-like API for Option[/-] [+]Tracking issue: Provide Entry-like API for Option[/+] on Feb 6, 2017
  9. oberien commented on May 22, 2017

    @oberien
    Contributor

    As this issue is quite old already (over 3 months), would it be possible to stabilize these two functions soon?

  10. 7 remaining items

  11. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    on Jun 6, 2017
  12. rfcbot commented on Jun 16, 2017

    @rfcbot

    The final comment period is now complete.

  13. dhardy commented on Jun 26, 2017

    @dhardy
    Contributor

    This isn't Entry-like. If it was, it would be possible to do the following:

    match opt_value.entry() {
        VacantEntry(entry) => entry.insert(x),
        OccupiedEntry(entry) => { assert_eq!(entry.get(), x); }
    }
    
  14. SimonSapin commented on Jun 27, 2017

    @SimonSapin
    Contributor

    @dhardy That particular example can be written as:

    match opt_value {
        ref mut entry @ None => *entry = Some(x),
        Some(ref entry) => assert_eq!(entry, x),
    }
  15. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Jul 22, 2017
  16. added a commit that references this issue on Aug 12, 2017
    d85df50
  17. raphlinus commented on Nov 1, 2017

    @raphlinus
    Contributor

    To save time for people researching this, the feature landed in stable at Rust 1.20 (source: 64c1b23).

  18. shepmaster commented on Nov 1, 2017

    @shepmaster
    Member

    @raphlinus I think that commit is wrong :-) The function in question doesn't show up in the 1.20.0 docs but does in the 1.21.0 docs. Nope, never mind; I just forgot how to search, sigh.

    GitHub does say that commit is only in the 1.21.0 tag though...

  19. shepmaster commented on Nov 1, 2017

    @shepmaster
    Member

    Ah, the 1.20.0 commit was a backport, thus why it's a different tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions