Skip to content

Commit 1ab914d

Browse files
committed
Write option::chain and result::chain with match move
Closes #3590
1 parent 5a8ba07 commit 1ab914d

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

src/libcore/option.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ pub pure fn chain<T, U>(opt: Option<T>,
119119
* function that returns an option.
120120
*/
121121

122-
// XXX write with move match
123-
if opt.is_some() {
124-
f(unwrap(opt))
125-
} else {
126-
None
122+
match move opt {
123+
Some(move t) => f(t),
124+
None => None
127125
}
128126
}
129127

src/libcore/result.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
105105
*/
106106
pub fn chain<T, U: Copy, V: Copy>(res: Result<T, V>, op: fn(t: T)
107107
-> Result<U, V>) -> Result<U, V> {
108-
// XXX: Should be writable with move + match
109-
if res.is_ok() {
110-
op(unwrap(res))
111-
} else {
112-
Err(unwrap_err(res))
108+
match move res {
109+
Ok(move t) => op(t),
110+
Err(move e) => Err(e)
113111
}
114112
}
115113

0 commit comments

Comments
 (0)