Skip to content

Commit 625ca7a

Browse files
author
blake2-ppc
committed
option: Add .chain_mut_ref()
.chain_mut_ref() is the mutable alternative to .chain_ref(). A use case example for this method is extraction of an optional value from an Option<Container> value.
1 parent 5991c60 commit 625ca7a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/libstd/option.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ impl<T> Option<T> {
159159
}
160160
}
161161

162+
/// Update an optional value by optionally running its content by mut reference
163+
/// through a function that returns an option.
164+
#[inline]
165+
pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>)
166+
-> Option<U> {
167+
match *self {
168+
Some(ref mut x) => f(x),
169+
None => None
170+
}
171+
}
172+
162173
/// Filters an optional value using given function.
163174
#[inline(always)]
164175
pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {

0 commit comments

Comments
 (0)