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.
This commit is contained in:
parent
5991c60750
commit
625ca7afe4
1 changed files with 11 additions and 0 deletions
|
|
@ -159,6 +159,17 @@ impl<T> Option<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Update an optional value by optionally running its content by mut reference
|
||||
/// through a function that returns an option.
|
||||
#[inline]
|
||||
pub fn chain_mut_ref<'a, U>(&'a mut self, f: &fn(x: &'a mut T) -> Option<U>)
|
||||
-> Option<U> {
|
||||
match *self {
|
||||
Some(ref mut x) => f(x),
|
||||
None => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Filters an optional value using given function.
|
||||
#[inline(always)]
|
||||
pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue