Write option::chain and result::chain with `match move`

Closes #3590
This commit is contained in:
Tim Chevalier 2012-10-11 14:14:46 -07:00
parent 5a8ba073bc
commit 1ab914df1d
2 changed files with 6 additions and 10 deletions

View File

@ -119,11 +119,9 @@ pub pure fn chain<T, U>(opt: Option<T>,
* function that returns an option.
*/
// XXX write with move match
if opt.is_some() {
f(unwrap(opt))
} else {
None
match move opt {
Some(move t) => f(t),
None => None
}
}

View File

@ -105,11 +105,9 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
*/
pub fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(t: T)
-> Result<U, V>) -> Result<U, V> {
// XXX: Should be writable with move + match
if res.is_ok() {
op(unwrap(res))
} else {
Err(unwrap_err(res))
match move res {
Ok(move t) => op(t),
Err(move e) => Err(e)
}
}