diff options
| author | blake2-ppc <blake2-ppc> | 2013-07-20 19:19:13 +0200 |
|---|---|---|
| committer | blake2-ppc <blake2-ppc> | 2013-07-20 19:19:13 +0200 |
| commit | 625ca7afe4fb50758215d623d45de84a5f4290ae (patch) | |
| tree | 53f974724daf494d699b658a7963e88a53f5c435 /src/libstd | |
| parent | 5991c607501595b32998a3367171ca42f5d8ef0b (diff) | |
| download | rust-625ca7afe4fb50758215d623d45de84a5f4290ae.tar.gz rust-625ca7afe4fb50758215d623d45de84a5f4290ae.zip | |
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.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index b68c67e8903..f116c1127ca 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -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> { |
