refac(rust): make create phone solution cleaner
This commit is contained in:
parent
0ba20655f0
commit
1be5b8904d
1 changed files with 15 additions and 2 deletions
|
@ -1,6 +1,19 @@
|
||||||
|
const PHONE_MASK: &str = "(___) ___-____";
|
||||||
|
const ZERO: u8 = '0' as u8;
|
||||||
|
|
||||||
fn create_phone_number(numbers: &[u8]) -> String {
|
fn create_phone_number(numbers: &[u8]) -> String {
|
||||||
let numbers = numbers.iter().map(|i| i.to_string()).collect::<String>();
|
let mut position = 0;
|
||||||
format!("({}) {}-{}", &numbers[..3], &numbers[3..6], &numbers[6..])
|
PHONE_MASK
|
||||||
|
.chars()
|
||||||
|
.map(|c| match c {
|
||||||
|
'_' => {
|
||||||
|
let c = (numbers[position] + ZERO) as char;
|
||||||
|
position += 1;
|
||||||
|
c
|
||||||
|
}
|
||||||
|
c => c,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue