Skip to content

Option should have method get_or_default #55042

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Boscop opened this issue Oct 13, 2018 · 4 comments
Closed

Option should have method get_or_default #55042

Boscop opened this issue Oct 13, 2018 · 4 comments

Comments

@Boscop
Copy link

Boscop commented Oct 13, 2018

There is some symmetry between the methods of HashMap's Entry and Option, e.g.
https://doc.rust-lang.org/nightly/std/collections/hash_map/enum.Entry.html#method.or_insert
https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.get_or_insert

https://doc.rust-lang.org/nightly/std/collections/hash_map/enum.Entry.html#method.or_insert_with
https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.get_or_insert_with

But the analogue of Entry's or_default is missing from Option:
Option should have a method get_or_default, that does conceptually the same as or_default by doing self.get_or_insert_with(|| Default::default()) (returns &mut T).

(This use case occurs often in some projects of mine..)

@clarfonthey
Copy link
Contributor

This is already available as Option::unwrap_or_default.

@Centril
Copy link
Contributor

Centril commented Oct 13, 2018

Closing as completed.

@Centril Centril closed this as completed Oct 13, 2018
@Boscop
Copy link
Author

Boscop commented Oct 14, 2018

@clarcharr It's not the same because it consumes the Option instead of mutating it.
One can do opt = Some(opt.unwrap_or_default()); but then one doesn't have a &mut T to work with, that refers to the Some(..)-content of the Option!
In the use-cases where I needed this, I need to have the &mut T from that point on, just like with Entry's or_default. (In other cases, yes, I use unwrap_or_default.)
So I think it's still worth adding the get_or_default method that returns a &mut T to the contents of the Option.
(In fact, I need this behavior more often with Option than with HashMap's Entry.)
@Centril Can we please re-open this issue? :)

@CasperN
Copy link

CasperN commented Aug 5, 2020

Bump, please consider reopening this.

self.get_or_default()

is 20 characters shorter than

self.get_or_insert_with(Default::default)

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 9, 2021
…=joshtriplett

Add Option::get_or_default

Tracking issue: rust-lang#82901

The original issue is rust-lang#55042, which was closed, but for an invalid reason (see discussion there). Opening this to reconsider (I hope that's okay). It seems like the only gap for `Option` being "entry-like".

I ran into a need for this method where I had a `Vec<Option<MyData>>` and wanted to do `vec[n].get_or_default().my_data_method()`. Using an `Option` as an inner component of a data structure is probably where the need for this will normally arise.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 9, 2021
…=joshtriplett

Add Option::get_or_default

Tracking issue: rust-lang#82901

The original issue is rust-lang#55042, which was closed, but for an invalid reason (see discussion there). Opening this to reconsider (I hope that's okay). It seems like the only gap for `Option` being "entry-like".

I ran into a need for this method where I had a `Vec<Option<MyData>>` and wanted to do `vec[n].get_or_default().my_data_method()`. Using an `Option` as an inner component of a data structure is probably where the need for this will normally arise.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Mar 9, 2021
…=joshtriplett

Add Option::get_or_default

Tracking issue: rust-lang#82901

The original issue is rust-lang#55042, which was closed, but for an invalid reason (see discussion there). Opening this to reconsider (I hope that's okay). It seems like the only gap for `Option` being "entry-like".

I ran into a need for this method where I had a `Vec<Option<MyData>>` and wanted to do `vec[n].get_or_default().my_data_method()`. Using an `Option` as an inner component of a data structure is probably where the need for this will normally arise.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants