Skip to content

Commit d367482

Browse files
committed
auto merge of #12871 : aochagavia/rust/Optimize-while_some, r=alexcrichton
The old 'while' needed to match 2 times for each iteration. With the new 'loop' there is just one match needed. I have also replaced 'blk' by 'f' to be more consistent with parameter names in other functions that are implemented for Option<T>
2 parents 29756a3 + 6ff207b commit d367482

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/libstd/option.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,13 @@ impl<T> Option<T> {
295295

296296
/// Applies a function zero or more times until the result is `None`.
297297
#[inline]
298-
pub fn while_some(self, blk: |v: T| -> Option<T>) {
298+
pub fn while_some(self, f: |v: T| -> Option<T>) {
299299
let mut opt = self;
300-
while opt.is_some() {
301-
opt = blk(opt.unwrap());
300+
loop {
301+
match opt {
302+
Some(x) => opt = f(x),
303+
None => break
304+
}
302305
}
303306
}
304307

0 commit comments

Comments
 (0)