From 1be5b8904d5f04a9465c55a1795dd4ee39309ce4 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 29 Oct 2021 23:12:43 +0300 Subject: [PATCH] refac(rust): make create phone solution cleaner --- rust/tests/4_create_phone_number.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/rust/tests/4_create_phone_number.rs b/rust/tests/4_create_phone_number.rs index c4ec151..d492a89 100644 --- a/rust/tests/4_create_phone_number.rs +++ b/rust/tests/4_create_phone_number.rs @@ -1,6 +1,19 @@ +const PHONE_MASK: &str = "(___) ___-____"; +const ZERO: u8 = '0' as u8; + fn create_phone_number(numbers: &[u8]) -> String { - let numbers = numbers.iter().map(|i| i.to_string()).collect::(); - format!("({}) {}-{}", &numbers[..3], &numbers[3..6], &numbers[6..]) + let mut position = 0; + PHONE_MASK + .chars() + .map(|c| match c { + '_' => { + let c = (numbers[position] + ZERO) as char; + position += 1; + c + } + c => c, + }) + .collect() } #[test]