From b9949eff67c2f425abb2bb2013872f6838e8d880 Mon Sep 17 00:00:00 2001 From: mos Date: Mon, 3 Mar 2025 15:33:05 +0100 Subject: [PATCH] improve read_rle --- src/lib.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3adfc3b..27e452c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -168,20 +168,20 @@ impl Level { } fn read_rle(map: &str) -> std::result::Result { - let mut n: usize = 1; - Self::read( map.chars() - .filter_map(|c| match c { - '0'..='9' => { - n = c.to_digit(10).unwrap() as usize; - None - } - '|' => Some("\n".into()), - _ => Some(c.to_string().repeat(n)), + .scan(1, |n, c| { + Some(match c { + '0'..='9' => { + *n = c.to_digit(10).unwrap() as usize; + None + } + '|' => Some("\n".to_string()), + _ => Some(c.to_string().repeat(*n)), + }) }) - .collect::>() - .join("") + .flatten() + .collect::() .as_bytes(), ) }