refac(rust): make create phone solution cleaner

This commit is contained in:
Dmitriy Pleshevskiy 2021-10-29 23:12:43 +03:00
parent 0ba20655f0
commit 1be5b8904d

View file

@ -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::<String>();
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]