diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2013-06-29 22:19:32 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2013-06-29 22:21:37 -0700 |
| commit | 208eb0f8cbbf6c30d5a4de957bee742c22c307db (patch) | |
| tree | e9bc0b5c94eead9a6494fe31579d239e10f69821 /src/libstd | |
| parent | 439b13f071a4a884ea8645670df83162ffcf129f (diff) | |
| download | rust-208eb0f8cbbf6c30d5a4de957bee742c22c307db.tar.gz rust-208eb0f8cbbf6c30d5a4de957bee742c22c307db.zip | |
Implement `map_mut` on the Option type
Closes #7394
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/option.rs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 64381231258..fb9962f8a44 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -161,7 +161,7 @@ impl<T> Option<T> { /// Filters an optional value using given function. #[inline(always)] - pub fn filtered<'a>(self, f: &fn(t: &'a T) -> bool) -> Option<T> { + pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> { match self { Some(x) => if(f(&x)) {Some(x)} else {None}, None => None @@ -170,10 +170,16 @@ impl<T> Option<T> { /// Maps a `some` value from one type to another by reference #[inline] - pub fn map<'a, U>(&self, f: &fn(&'a T) -> U) -> Option<U> { + pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> { match *self { Some(ref x) => Some(f(x)), None => None } } + /// Maps a `some` value from one type to another by a mutable reference + #[inline] + pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> { + match *self { Some(ref mut x) => Some(f(x)), None => None } + } + /// As `map`, but consumes the option and gives `f` ownership to avoid /// copying. #[inline] |
