-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Option should have method get_or_default
#55042
Comments
This is already available as |
Closing as completed. |
@clarcharr It's not the same because it consumes the |
Bump, please consider reopening this. self.get_or_default() is 20 characters shorter than self.get_or_insert_with(Default::default) |
…=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.
…=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.
…=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.
There is some symmetry between the methods of
HashMap
'sEntry
andOption
, 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
'sor_default
is missing fromOption
:Option
should have a methodget_or_default
, that does conceptually the same asor_default
by doingself.get_or_insert_with(|| Default::default())
(returns&mut T
).(This use case occurs often in some projects of mine..)
The text was updated successfully, but these errors were encountered: