-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Tracking issue: Provide Entry-like API for Option #39288
Comments
Isn't this just Edit: These don't modify the original |
indeed |
This implements rust-lang#39288. Thanks to @steveklabnik and @oli-obk for their review comments :)
Provide Entry-like API for Option This implements rust-lang#39288. I am wondering whether to use std::intrinsics::unreachable!() here. Both seems fine to me (the second match optimizes away in release mode).
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. |
Ah, right. Maybe you could put "Tracking issue" in the title. |
As this issue is quite old already (over 3 months), would it be possible to stabilize these two functions soon? |
@rfcbot merge |
@rfcbot fcp merge |
Team member @brson has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
To be clear, the APIs proposed here for stabilization are: impl<T> Option<T> {
fn get_or_insert(&mut self, v: T) -> &mut T;
fn get_or_insert_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T;
} |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
This isn't
|
@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),
} |
Stabilized: * `Option::get_or_insert` * `Option::get_or_insert_with` Closes rust-lang#39288
To save time for people researching this, the feature landed in stable at Rust 1.20 (source: 64c1b23). |
@raphlinus GitHub does say that commit is only in the 1.21.0 tag though... |
Ah, the 1.20.0 commit was a backport, thus why it's a different tag. |
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.
The text was updated successfully, but these errors were encountered: