Skip to content

Commit 60a96ca

Browse files
committed
more tests in option.insert, code cleaning in option
Code cleaning made according to suggestions in discussion on PR ##77392 impacts insert, get_or_insert and get_or_insert_with.
1 parent e8df2a4 commit 60a96ca

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

library/core/src/option.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -574,17 +574,22 @@ impl<T> Option<T> {
574574
/// ```
575575
/// #![feature(option_insert)]
576576
///
577-
/// let mut o = None;
578-
/// let v = o.insert(3);
579-
/// assert_eq!(*v, 3);
577+
/// let mut opt = None;
578+
/// let val = opt.insert(1);
579+
/// assert_eq!(*val, 1);
580+
/// assert_eq!(opt.unwrap(), 1);
581+
/// let val = opt.insert(2);
582+
/// assert_eq!(*val, 2);
583+
/// *val = 3;
584+
/// assert_eq!(opt.unwrap(), 3);
580585
/// ```
581586
#[inline]
582-
#[unstable(feature = "option_insert", reason = "new API", issue = "none")]
583-
pub fn insert(&mut self, v: T) -> &mut T {
584-
*self = Some(v);
587+
#[unstable(feature = "option_insert", reason = "newly added", issue = "none")]
588+
pub fn insert(&mut self, val: T) -> &mut T {
589+
*self = Some(val);
585590

586-
match *self {
587-
Some(ref mut v) => v,
591+
match self {
592+
Some(v) => v,
588593
// SAFETY: the code above just filled the option
589594
None => unsafe { hint::unreachable_unchecked() },
590595
}
@@ -839,8 +844,8 @@ impl<T> Option<T> {
839844
/// ```
840845
#[inline]
841846
#[stable(feature = "option_entry", since = "1.20.0")]
842-
pub fn get_or_insert(&mut self, v: T) -> &mut T {
843-
self.get_or_insert_with(|| v)
847+
pub fn get_or_insert(&mut self, val: T) -> &mut T {
848+
self.get_or_insert_with(|| val)
844849
}
845850

846851
/// Inserts a value computed from `f` into the option if it is [`None`], then
@@ -867,8 +872,8 @@ impl<T> Option<T> {
867872
*self = Some(f());
868873
}
869874

870-
match *self {
871-
Some(ref mut v) => v,
875+
match self {
876+
Some(v) => v,
872877
// SAFETY: a `None` variant for `self` would have been replaced by a `Some`
873878
// variant in the code above.
874879
None => unsafe { hint::unreachable_unchecked() },

0 commit comments

Comments
 (0)