From 625ca7afe4fb50758215d623d45de84a5f4290ae Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Sat, 20 Jul 2013 19:19:13 +0200 Subject: [PATCH] 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 value. --- src/libstd/option.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index b68c67e8903b..f116c1127ca9 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -159,6 +159,17 @@ impl Option { } } + /// 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) + -> Option { + 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 {