Skip to content

Commit 208eb0f

Browse files
committed
Implement map_mut on the Option type
Closes #7394
1 parent 439b13f commit 208eb0f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/libstd/option.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<T> Option<T> {
161161

162162
/// Filters an optional value using given function.
163163
#[inline(always)]
164-
pub fn filtered<'a>(self, f: &fn(t: &'a T) -> bool) -> Option<T> {
164+
pub fn filtered(self, f: &fn(t: &T) -> bool) -> Option<T> {
165165
match self {
166166
Some(x) => if(f(&x)) {Some(x)} else {None},
167167
None => None
@@ -170,10 +170,16 @@ impl<T> Option<T> {
170170

171171
/// Maps a `some` value from one type to another by reference
172172
#[inline]
173-
pub fn map<'a, U>(&self, f: &fn(&'a T) -> U) -> Option<U> {
173+
pub fn map<'a, U>(&'a self, f: &fn(&'a T) -> U) -> Option<U> {
174174
match *self { Some(ref x) => Some(f(x)), None => None }
175175
}
176176

177+
/// Maps a `some` value from one type to another by a mutable reference
178+
#[inline]
179+
pub fn map_mut<'a, U>(&'a mut self, f: &fn(&'a mut T) -> U) -> Option<U> {
180+
match *self { Some(ref mut x) => Some(f(x)), None => None }
181+
}
182+
177183
/// As `map`, but consumes the option and gives `f` ownership to avoid
178184
/// copying.
179185
#[inline]

0 commit comments

Comments
 (0)