Skip to content

Tracking Issue for Option::get_or_insert_default #82901

Closed
@camsteffen

Description

@camsteffen
Contributor

Feature gate: #![feature(option_get_or_insert_default)]

This is a tracking issue for adding Option::get_or_insert_default.

Basically, it is a shorthand for option.get_or_insert_with(Default::default). It differs with unwrap_or_default since it does not consume the Option. This is useful, for example, when you have an Option as a struct field or inside a data structure like Vec<Option<T>>.

Public API

impl<T> Option<T> {
    pub fn get_or_insert_default(&mut self) -> &mut T where T: Default;
}

Steps / History

Unresolved Questions

  • None yet.

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Mar 8, 2021
m-ou-se

m-ou-se commented on Mar 9, 2021

@m-ou-se
Member

It's not obvious to me at all from the name that this modifies the Option. It sounds like it'd be the same as unwrap_or_default. Something like get_or_insert_default would be more consistent with the other method names.

camsteffen

camsteffen commented on Mar 9, 2021

@camsteffen
ContributorAuthor

Hmm I hadn't considered that.

added 2 commits that reference this issue on Mar 9, 2021

Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…

79efa62

Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…

f0c61c8
added a commit that references this issue on Mar 9, 2021

Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…

1c3fea2
changed the title [-]Tracking Issue for `Option::get_or_default`[/-] [+]Tracking Issue for `Option::get_or_insert_default`[/+] on Mar 10, 2021
added a commit that references this issue on Mar 10, 2021

Rollup merge of rust-lang#82977 - camsteffen:opt-get-insert-def, r=m-…

e583132
sffc

sffc commented on Dec 31, 2021

@sffc

+1 on the name get_or_insert_default

I wanted to use this today, but it's still experimental. 😆

insou22

insou22 commented on Apr 7, 2022

@insou22

+1 -- was looking for this exact method today

djc

djc commented on Jul 7, 2022

@djc
Contributor

@rustbot ping libs

Let's see if this works now? Would be nice to have this FCP'ed.

cc @joshtriplett

37 remaining items

Loading
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

    A-result-optionArea: Result and Option combinatorsC-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @imp@djc@joshtriplett@BurntSushi@m-ou-se

      Issue actions

        Tracking Issue for `Option::get_or_insert_default` · Issue #82901 · rust-lang/rust