From 17fdff594d12ad80d44ea413b98d6fc888fd541a Mon Sep 17 00:00:00 2001 From: kageru Date: Sat, 18 May 2019 15:16:59 +0200 Subject: [PATCH] =?UTF-8?q?refactored=20is=5Fsubsequence=20(thanks,=20K?= =?UTF-8?q?=C3=BCbi)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib.rs | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8b9d305..1a8af80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,29 +18,20 @@ fn is_subsequence(needle: &str, string: &str) -> bool { // we need to explicitly convert here to iterate over chars // (UTF-8, so possibly multiple bytes long) let mut chars = string.chars(); - let needle_chars = needle.chars(); - for nc in needle_chars { - loop { - match chars.next() { - Some(current) => { - // if the character matches, break and continue with the next nc - if current == nc { - break; - } - } - _ => return false + 'needle: for nc in needle.chars() { + for c in &mut chars { + if c == nc { + continue 'needle; } } + return false; } true } #[cfg(test)] mod tests { - use crate::{ - filter_substring, - filter_subsequence - }; + use crate::{filter_subsequence, filter_substring}; #[test] fn test_filter_substring_single() {