Change:
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 asref()/asmut() 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}orinsert{,with}() marrying the get functions (for example in hashmaps) for getting references with the orinsert Entry like functions.
Isn't this just and ? Edit: These don't modify the original which I guess it what you wanted
indeed
See also and . Also, this seems fixed by .
AIUI, this is not fixed by , 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?
merge
fcp merge
Team member has proposed to merge this. The next step is review by the rest of the tagged teams: [x] [x] [x] [x] [x] [x] [x] 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 for info about what commands tagged team members can give me.
To be clear, the APIs proposed here for stabilization are:
:bell: This is now entering its final comment period, as per the . :bell:
The final comment period is now complete.
This isn't -like. If it was, it would be possible to do the following:
That particular example can be written as: rust match optvalue { ref mut entry @ None =*entry = Some(x), Some(ref entry) =asserteq!(entry, x), }
To save time for people researching this, the feature landed in stable at Rust 1.20 (source: ).
<sI think that commit is wrong :-) The function in question doesn't show up in but does in . </sNope, never mind; I just forgot how to search, sigh. GitHub does say that commit is only in the 1.21.0 tag though...
Ah, , thus why it's a different tag.

Discussion:
} ///////////////////////////////////////////////////////////////////////// <ins> // Entry-like operations to insert if None and return a reference ///////////////////////////////////////////////////////////////////////// /// Inserts `v` into the option if it is `None`, then /// returns a mutable reference to the contained value. /// /// # Examples /// /// ``` /// #![feature(option_entry)] /// /// let mut x = None; /// /// { ///     let y: &mut u32 = x.get_or_insert(5); ///     assert_eq!(y, &5); /// ///     *y = 7; /// } /// /// assert_eq!(x, Some(7)); /// ``` #[inline] #[unstable(feature = "option_entry", issue = "39288")] pub fn get_or_insert(&mut self, v: T) -> &mut T { match *self { None => *self = Some(v), _ => (), } match *self { Some(ref mut v) => v, _ => unreachable!(), } } /// Inserts a value computed from `f` into the option if it is `None`, then /// returns a mutable reference to the contained value. /// /// # Examples /// /// ``` /// #![feature(option_entry)] /// /// let mut x = None; /// /// { ///     let y: &mut u32 = x.get_or_insert_with(|| 5); ///     assert_eq!(y, &5); /// ///     *y = 7; /// } /// /// assert_eq!(x, Some(7)); /// ``` #[inline] #[unstable(feature = "option_entry", issue = "39288")] pub fn get_or_insert_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T { match *self { None => *self = Some(f()), _ => (), } match *self { Some(ref mut v) => v, _ => unreachable!(), } } ///////////////////////////////////////////////////////////////////////// </ins> // Misc /////////////////////////////////////////////////////////////////////////