diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 2b9794987ee0..0ad0cb6045bc 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -37,12 +37,20 @@ pure fn expect(opt: option, reason: ~str) -> T { alt opt { some(x) { x } none { fail reason; } } } -pure fn map(opt: option, f: fn(T) -> U) -> option { +pure fn map(opt: option, f: fn(T) -> U) -> option { //! Maps a `some` value from one type to another alt opt { some(x) { some(f(x)) } none { none } } } +pure fn map_consume(-opt: option, f: fn(-T) -> U) -> option { + /*! + * As `map`, but consumes the option and gives `f` ownership to avoid + * copying. + */ + if opt.is_some() { some(f(option::unwrap(opt))) } else { none } +} + pure fn chain(opt: option, f: fn(T) -> option) -> option { /*! * Update an optional value by optionally running its content through a @@ -128,7 +136,7 @@ impl extensions for option { /// Returns true if the option contains some value pure fn is_some() -> bool { is_some(self) } /// Maps a `some` value from one type to another - pure fn map(f: fn(T) -> U) -> option { map(self, f) } + pure fn map(f: fn(T) -> U) -> option { map(self, f) } } impl extensions for option {