Closed
Description
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
- Original issue: Option should have method
get_or_default
#55042 - Implementation: Add Option::get_or_default #82849
- Renamed from
get_or_default
toget_or_insert_default
: RenameOption::get_or_default
toget_or_insert_default
#82977 - Final commenting period (FCP)
- Stabilization PR
Unresolved Questions
- None yet.
Metadata
Metadata
Assignees
Labels
Area: Result and Option combinatorsCategory: An issue tracking the progress of sth. like the implementation of an RFCRelevant to the library API team, which will review and decide on the PR/issue.This issue / PR is in PFCP or FCP with a disposition to merge it.The final comment period is finished for this PR / Issue.
Activity
m-ou-se commentedon Mar 9, 2021
It's not obvious to me at all from the name that this modifies the
Option
. It sounds like it'd be the same asunwrap_or_default
. Something likeget_or_insert_default
would be more consistent with the other method names.camsteffen commentedon Mar 9, 2021
Hmm I hadn't considered that.
Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…
Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…
Rollup merge of rust-lang#82849 - camsteffen:option-get-or-default, r…
Option::get_or_default
toget_or_insert_default
#82977[-]Tracking Issue for `Option::get_or_default`[/-][+]Tracking Issue for `Option::get_or_insert_default`[/+]Rollup merge of rust-lang#82977 - camsteffen:opt-get-insert-def, r=m-…
sffc commentedon Dec 31, 2021
+1 on the name
get_or_insert_default
I wanted to use this today, but it's still experimental. 😆
insou22 commentedon Apr 7, 2022
+1 -- was looking for this exact method today
djc commentedon Jul 7, 2022
@rustbot ping libs
Let's see if this works now? Would be nice to have this FCP'ed.
cc @joshtriplett
37 remaining items