From 6ff207b415233072bff2efb4b7b0f5fe883348e5 Mon Sep 17 00:00:00 2001 From: aochagavia Date: Thu, 13 Mar 2014 21:28:36 +0100 Subject: [PATCH] Refactored while_some (libstd/option.rs) 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 --- src/libstd/option.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libstd/option.rs b/src/libstd/option.rs index 86f8c143a9e99..5b5138c6c12c5 100644 --- a/src/libstd/option.rs +++ b/src/libstd/option.rs @@ -295,10 +295,13 @@ impl Option { /// Applies a function zero or more times until the result is `None`. #[inline] - pub fn while_some(self, blk: |v: T| -> Option) { + pub fn while_some(self, f: |v: T| -> Option) { let mut opt = self; - while opt.is_some() { - opt = blk(opt.unwrap()); + loop { + match opt { + Some(x) => opt = f(x), + None => break + } } }